“Request for memberâ€A†in “B†is of non-class type â€Câ€
Hi all,
can someone explain why i get the error “Request for memberâ€A†in “B†is of non-class type â€C†if i use a constructor without parameters C B(); and why this error dissappear if i use C B(1); (With 1 as dummy param which is not used anywhere else)
By the way the appropriate header file is always included and the code for the constructors are identical.
Thanks
dexli
Re: “Request for memberâ€A†in “B†is of non-class type â€Câ€
For constructor without parameter use:
Code:
CLASS_NAME variableName; //without ()
Re: “Request for memberâ€A†in “B†is of non-class type â€Câ€
Show the code, which compiler is used ?
This compiles without complains with g++ 4.5.2
Code:
class Test{
public:
Test(){
}
Test(int){
}
};
int main(){
Test t;
Test t1();
Test t2(1);
return 0;
}
Re: “Request for memberâ€A†in “B†is of non-class type â€Câ€
@stampede, it compiles, but if you modify the code to "track" the constructors calls
Code:
#include <iostream>
class Test{
public:
Test(){
std::cout << "Test()\n";
}
Test(int){
std::cout << "Test(int)\n";
}
};
int main(){
Test t;
Test t1();
Test t2(1);
std::cin.get();
return 0;
}
You will see that only one Test() and one Test(int), that is because Test t1(); is "compiled" as a function declaration (a function called t1 that returns a Test and takes no parameter)
//i can't test right now, but as far as i know this is the standard C++ behavior.
Re: “Request for memberâ€A†in “B†is of non-class type â€Câ€
Hi all,
thanks for the reply but it seems that i do not make clear enough what's the problem.
Code:
class Test{
public:
unsigned int x;
Test(){
x = 1;
}
Test(unsigned int y)
{
x = 1;
}
}
int main(){
Test t1();
Test t2(1);
unsigned int c =t2.x; // no problem
unsigned int d =t1.x; // compiler error
return 0;
}
Re: “Request for memberâ€A†in “B†is of non-class type â€Câ€
@Zlatomir: you are right, tested.
Test t1(); is the same as Test t1(void);, so its a declaration indeed.
It's like that when you use stack, you can use new Test(); and new Test; if allocating with operator new.
@up: yes, because t1 is function (declaration)
Re: “Request for memberâ€A†in “B†is of non-class type â€Câ€
That error is because t1 declared: Test t1(); is not an Test instance.
If you use Test t1; //without () it would work.
LE: stampede was faster ;)
The empty parentheses for default constructor can be used when you create objects on the heap, so:
Code:
Test *ptr = new Test(); //is ok
//so is this
Test *ptr = new Test; //ok too
Isn't C++ a beautiful language?
Re: “Request for memberâ€A†in “B†is of non-class type â€Câ€
Yeah, it's a quirk of the language. Because of the (weird) order that C/C++ evaluates and assigns type values, the "Test t1();" statement is interpreted as a call, not a declaration.
It's just one of those "gotchas" that one learns of (sometimes several times) over the years.
Re: “Request for memberâ€A†in “B†is of non-class type â€Câ€
@DanH: trying to call a function without definition will trigger a linker error about some unresolved external, so the is interpreted by the compiler as a function declaration, not as a function call.
Re: “Request for memberâ€A†in “B†is of non-class type â€Câ€
Whatever -- you don't get what you want, and usually get a compile error.
Re: “Request for memberâ€A†in “B†is of non-class type â€Câ€
Thanks for the hint without the braces after the call.