Help with classes and member variables in C++
Ok I have two classes: Image and Scene. Now in the Image header file, I defined three private variables: xcoord, ycoord, and index (as well as their respective public getter methods).
I have another class named Scene. Scene is not a subclass of Image. Scene has two member variables: int maximum
and Image **images
. Now in Scene, I have some methods that attempt to access the member variables of the Image class. For example:
int beginX =this->images[i].getXcoord;
int beginY =this->images[i].getYcoord;
However, I get the following errors:
error: request for member ‘getXcoord’ in ‘*(((Image**)((const Scene*)this)->Scene::images) + ((Image**)(((long unsigned int)i) * 8ul)))’, which is of non-class type ‘Image*’
scene.cpp:135: error: request for member ‘getYcoord’ in ‘*(((Image**)((const Scene*)this)->Scene::images) + ((Image**)(((long unsigned int)i) * 8ul)))’, which is of non-class type ‘Image*’
In my scene.cpp file, I have included scene.h which includes image.h, so I'm pretty sure everything is properly linked. Is it apparent what my problem 开发者_StackOverflow中文版is or will I have to provide more information?
You want to call methods so try:
int beginX = this->images[i]->getXcoord();
int beginY = this->images[i]->getYcoord();
otherwise the compiler is looking for a member variable and not a getter method
If this->images
is an Image**
, then this->images[i]
is an Image*
.
Replace the dots with arrows.
The problem is the images array holds pointers to classes
try
int beginX =this->images[i]->getXcoord;
also if getXcoord is a function you need to call it like this
int beginX =this->images[i]->getXcoord();
lastly you dont need this->
its implied so use
int beginX = images[i]->getXcoord();
DC
There are two issues. It should be:
int beginX = this->images[i]->getXcoord();
The error message isn't allowing the '.' operator on the Image* which is not a class-type object.
精彩评论