PDA

View Full Version : “Request for member”A” in “B” is of non-class type ”C”



dexli
21st April 2011, 09:38
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

Zlatomir
21st April 2011, 09:41
For constructor without parameter use:


CLASS_NAME variableName; //without ()

stampede
21st April 2011, 10:02
Show the code, which compiler is used ?
This compiles without complains with g++ 4.5.2


class Test{
public:
Test(){
}
Test(int){
}
};

int main(){
Test t;
Test t1();
Test t2(1);
return 0;
}

Zlatomir
21st April 2011, 10:18
@stampede, it compiles, but if you modify the code to "track" the constructors calls

#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.

dexli
21st April 2011, 10:29
Hi all,

thanks for the reply but it seems that i do not make clear enough what's the problem.



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;

}

stampede
21st April 2011, 10:32
@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)

Zlatomir
21st April 2011, 10:34
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:

Test *ptr = new Test(); //is ok
//so is this
Test *ptr = new Test; //ok too

Isn't C++ a beautiful language?

DanH
24th April 2011, 13:41
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.

Zlatomir
24th April 2011, 14:24
@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.

DanH
25th April 2011, 01:43
Whatever -- you don't get what you want, and usually get a compile error.

dexli
1st May 2011, 18:27
Thanks for the hint without the braces after the call.