Problem with temporary object instantiation
- From: "Chris Yoedhana" <ChrisYoedhana@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Sat, 28 Jan 2006 16:37:26 -0800
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
.
- Follow-Ups:
- Re: Problem with temporary object instantiation
- From: Nikolaos D. Bougalis
- Re: Problem with temporary object instantiation
- Prev by Date: msxml.h include problems
- Next by Date: Re: Problem with temporary object instantiation
- Previous by thread: msxml.h include problems
- Next by thread: Re: Problem with temporary object instantiation
- Index(es):
Relevant Pages
|