1

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.

7
  • What are you trying to do with foo->bar? In this case, foo is an instance of the ux class. ux doesn't have anything in it named "bar", which is why you're getting the error. Commented Nov 23, 2015 at 23:04
  • 1
    well, ux doesn't have a member called bar - so what did you expect foo->bar should do? Commented Nov 23, 2015 at 23:05
  • To learn about pointers, using new is not a requirement. foo ux(2, 2); foo* fptr = &ux; Then practice how to use pointers using fptr. Commented Nov 23, 2015 at 23:11
  • 3
    I think you do not understand what -> means. What you wrote is just "access a bar field in the object pointer by foo"... Commented Nov 23, 2015 at 23:11
  • 1
    it is hard for SO to answer your question since its not clear what you are trying to do. DO something simple first - then try to get clever. Use objects without pointers for example Commented Nov 23, 2015 at 23:17

3 Answers 3

1

The -> isn't used to access arbitrary pointers, it's used to access members of the class. The members of your ux class are width, height, and area. bar is a completely separate variable of type ux, and you would use it e.g. bar->width, bar->height, or bar->area().

Sign up to request clarification or add additional context in comments.

Comments

0

In line foo->bar you are giving an order to object foo to do function from class ux called bar . Your defined class ux does not have attribute/function defined in class. You should read this article about operators .Its maybe easier to learn more about C++ Classes and objects then dive into pointers.

Comments

0

I know it's possible to point to another pointer object with the arrow operator but it is not working here.

Yes it is possible. The right way to do it is:

foo = bar;

Since foo is a pointer, it is essentially a variable with an address as its value. To make another variable point to the same object, all you have to do is assign the address to the other variable.

As a side note, remember that every new should have a delete.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.