Getting address of variable in class

By alexmbcm

Today I tried to find out what was the problem with a CA program I was writing in c++ after the compiler gave me the following message: caArray is undefined. I was trying to pass the address of an entry in the caArray as a return value for a function. Other classes that modified the caArray didn’t give the error so I wasn’t sure what was happening. After some time I checked the definition and it was written just like the equivalent c definition but with the class name in front:

int <class name>::*<function name> 

But it should be defined like:

int *<class name>::<function name>

I know this seems like a stupid mistake for a c++ programmer but it might be a problem for someone starting with c++ from c, especially with the fact that the error message doesn’t say the definition is wrong.

2 Responses to “Getting address of variable in class”

  1. Gerall Says:

    With C++, you’ve got to remember to put your types at the beginning of a definition. This has bitten me more often than I care to admit!

    I wrote lots of automation code in C, then we started implementing different things in C++ for a VRML front-end (this was in the late 90’s). It took me quite a while to determine that the rule of thumb is to keep the type (and pointers and references count) in the first stage of a variable declaration.

    It’s not a stupid mistake; it’s a common thing for people coming from an ANSI C background.

  2. alexmbcm Says:

    Now I understand the issue better after reading C++ Primer Plus from Stephen Prata: when you type int * you have to consider the whole thing a type because the compiler needs to know what the pointer is pointing to.

Leave a Reply

You must be logged in to post a comment.