Request member non-class type… C++ October 4, 2006
Posted by Matt in Uncategorized.trackback
For those of you whom programm in C++, I have a little tip whne working with calsses. If you get an erroer that looks like this:
“Request for member”A” in “B” is of non-class type ”C”
Where A, B, and C ,may differ as to your actual code.
Whats happening(as far as I can tell…) is your calling things that don’t exist. We can only call things that exist….
Example(Plundered Geneously from below, so people strolling in don’t have to parse hundreds of comments)
In OBJECTS,
CObj Obj();//<–This the problem code.
If the CObj constructer has no parameters, It has no clue how to make such a thing, so it throws an error the nex t time you call for it.
If your making something without parameters, do it thusly.(Maybe. Try it at least…)
CObj Obj();//Better!
And that Might resolve the issue
In LINKED LISTS and POINTERS
float x = P.coord[0];//Problem Line
float x = P->coord[0];//Fixed.
I’m a bit away from Tech, so I openly admit to Bafflement on issues with Link Lists, Pointers, and the like. i found this solutioon below and fixed it my self, apparently. But I’m not sure what it all means.
And for you late comers, for a period of MONTHS, this comment was naught but
For those of you whom programm in C++, I have a little tip whne working with calsses. If you get an erroer that looks like this:
“Request for member”A” in “B” is of
Its a bit better now.
Hello?
…and the rest of what you were saying is…?
what you wanted to explain could be my problem i have now…
I honestly have no Idea of what I was saying. If you have any questions, definitly ask.
Maybe he had a heart attack while typing
I’m getting this error:
request for member ‘coord’ in ‘P’, which is of non-class type ‘const attr_point*’
here’s the code that likely pertains:
struct attr_point{
static const int START = 4;
static const int CONSTANT = 4;
static const int MAX = 24;
float coord[MAX];
};
int draw_bezier_curve(const attr_point P [], int degree)
{
float x = P.coord[0];
return 0;
}
try using a -> instead of a .
float x = P->coord[0];, not float x = P.coord[0];
Thank you
dude (matt), were you drunk when you tried to post this?
your comment helped me, so thanks anyway
this may also occur when you call some method Met() of some object Obj declared as:
CObj Obj(); //bad definition
Obj.Met(); //raised error
if a constructor of the CObj object has no parameters, you should NOT use brackets in Obj declaration, write it rather as:
CObj Obj;
Obj.Met();
Note: tested on MinGW
this may also occur when you call non-const method on const object.
In any case your topic has helped me)))).
in my case:
if your constructor does not take any arguments, don’t call the constructor function with brackets:
class myObject {
public: myObject(); ~myObject(); void doSomething();
};
…
myObject a();
a.doSomething();
– will raise an error on a.doSomething() line (for some reason). but what you need to change is the line:
myObject a;
the reason is, that
CObj Obj();
is declaration of function with no arguments returning object CObj
while
CObj Obj;
is creating of object of type CObj and calling its deafault constructor
About the -> versus the . operators, this is my understanding:
The ‘->’ operator automatically dereferences pointers, whether on the current object or its own private members.
The ‘.’ operator does not dereference, and thus the compiler cannot syntactically reach the desired method/member.
Structs frequently use the ‘->’ more than the ‘.’.
The dot operator can be used for dereferencing, but it is more work (and messy). For example, to set the value of x on the object p_ptr we have to do this:
(*p_ptr).x=0×8001010
instead of
p_ptr->x=0×8001010
CObj Obj();//<–This the problem code.
…
CObj Obj();//Better!
Those two lines look the same to me. Did you mean to omit the parenthesis from the second line?
what he tried to say is use Values to inicialize it
CObj Obj(type value,type value); // better
Hey Matt, what about an Aspirine? eheh! Thanks anyway, you solved my problem!!!
great! very useful!
thank you very much!
meh
meh2
meh3
meh4
Thanks
But the strings
CObj Obj();//<–This the problem code.
…
CObj Obj();//Better!
should fix.
Oh… I forgot… There is one else way of resolving this problem:
* i = new ();
i->method();
In my opinion declaration without brackets looks a little bit clumsy.
P. S. Sorry for my English if I make a mistake.
Hi guy,
You make a mistake in your blog.
CObj Obj();//Better!
SHOULD be changed to
CObj Obj;//Better!