Problem with temporary object instantiation

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi,

I cannot figure out why this code fails to compile. I use 2 files, main.h
and main.cpp to separate class declaration and definition.

in main.h

#define MAIN_H

#include <iostream>

class TestClass {
public:
TestClass(int val);
TestClass(float, int);
};

class WrapperClass {
public:
WrapperClass(const TestClass& testa);
int j;
};
#endif

in main.cpp

#include "main.h"

TestClass::TestClass(float a, int b)
{
std::cout<<"Invoking test class ctor with 2 param\n";
}

TestClass::TestClass(int val)
{
std::cout<<"TestClass val = "<<val<<std::endl;
}

WrapperClass::WrapperClass(const TestClass &aTest):j(0)
{
std::cout<<"Calling wrapper class constructor\n";
}

int main()
{
int i;
WrapperClass wrapper0(TestClass(i));
std::cout<<"wrapper0.j = "<<wrapper0.j<<std::endl;
return 0;
}

The error message is:
main.cpp(22) : error C2228: left of '.j' must have class/struct/union type
type is 'overloaded-function'

I can make the code compile by doing the following :
1. Replace i with an integer constant
WrapperClass wrapper0(TestClass(i)) --> WrapperClass
wrapper0(TestClass(100))

2. Use a scope resolution to call TestClass constructor
WrapperClass wrapper0(TestClass(i)); --> WrapperClass
wrapper0(TestClass::TestClass(i));

3. Removing the offending line. However I get this warning:
main.cpp(21) : warning C4930: 'WrapperClass wrapper0(TestClass)': prototyped
function not called (was a variable definition intended?)

The compiler is VC++ 7.

Sorry for the long message, and thank you for the help
.



Relevant Pages

  • Re: Problem with temporary object instantiation
    ... > I cannot figure out why this code fails to compile. ... > and main.cpp to separate class declaration and definition. ... > TestClass::TestClass(float a, int b) ... > WrapperClass wrapper0); ...
    (microsoft.public.vstudio.development)
  • Re: GCC difference in size of long int on Suse SLES9 / Suse Professional 10.0
    ... The kernel uses printf() with 'long int', ... I can't compile 'on-the-fly'. ... clashing with the hardware and any libs that you interact with. ... platform types build up their own int types. ...
    (comp.os.linux.development.apps)
  • Re: Im a C++ programmer, and Relfs X.CPP is good.
    ... >> and it doesn't compile either. ... home.cpp:93: implicit declaration of function `int fclose' ... You cannot change to your home directory through an external ...
    (comp.lang.lisp)
  • Re: Im a C++ programmer, and Relfs X.CPP is good.
    ... >> and it doesn't compile either. ... home.cpp:93: implicit declaration of function `int fclose' ... You cannot change to your home directory through an external ...
    (comp.unix.programmer)
  • Re: new order doubt
    ... Warning test2.c: 8 no type specified. ... Defaulting to int ... Tests probably come in several different types: this should compile ... Even Microsoft has problems with this obscure rules. ...
    (comp.lang.c)