I made a small program in c++ to learn more about pointer objects. However I keep getting an error that the pointer object "bar" is not a part of the class more specifically:
[Error] 'class ux' has no member named 'bar'
As shown below:
class ux {
private:
int width;
int height;
public:
ux(int, int);
~ux();
int area();
};
ux::ux(int a, int b)
{
width = a;
height = b;
}
ux::~ux(){}
int ux::area()
{
return (width * height);
}
int main(int argc, char** argv) {
ux * foo;
ux * bar;
foo = new ux(2, 2);
foo->bar = NULL;
cout << foo->area() << "\n";
cout << foo->bar << "\n";
}
Why is this happening? I know it's possible to point to another pointer object with the arrow operator but it is not working here.
---------------------Update-----------------------
I am not trying to do anything with foo->bar I just want to see the address or value printed out but I know that a pointer obj can point to another pointer obj. If someone can please specify a link that shows this technique in use I would greatly appreciate that.
foo->bar? In this case,foois an instance of theuxclass.uxdoesn't have anything in it named "bar", which is why you're getting the error.uxdoesn't have a member calledbar- so what did you expectfoo->barshould do?newis not a requirement.foo ux(2, 2); foo* fptr = &ux;Then practice how to use pointers usingfptr.->means. What you wrote is just "access a bar field in the object pointer by foo"...