Composition,private Class member variables and methods

This is a discussion on Composition,private Class member variables and methods within the C++ Programming forums, part of the General Programming Boards category; I have a class A which has a class B as a private member variable.Class b also has a class ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    Composition,private Class member variables and methods

    I have a class A which has a class B as a private member variable.Class b also has a class C as a private member variable.(A->B->C).. I am trying to call a method which has an implementation in class C but i keep getting an erroR saying the object of class C which i declared in class B is private when i try to write a method in class A. e.g B->C->setGreeting();
    class Country
    {
    public:
    void setPoints(int x,int y);
    private: City * city;
    }
    // i am having problems here with error Point * city p is private
    void Country::setPoints(int x,int y){
    city->p->setPoints(x,y);


    }
    class City
    {
    public:
    City();
    virtual ~City();
    protected:
    private:


    Point *p;
    };






    class Point
    {
    public:
    Point();
    virtual ~Point();
    void setPoints(int x,int y);


    protected:
    private:
    int x;
    int y;
    };
    void Point::setPoints(int xp,int yp){
    x=xp;
    y=yp;
    }
    Last edited by sigur47; 01-29-2012 at 10:43 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    It seems that the setGreeting member function of class C was not declared public.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by laserlight View Post
    It seems that the setGreeting member function of class C was not declared public.
    It is declared public

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Sigh. Of course. You are trying to access a private member of B from A, right?

    Just post the code. I hate misreading your prose. Code poetry is better.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by laserlight View Post
    Sigh. Of course. You are trying to access a private member of B from A, right?

    Just post the code. I hate misreading your prose. Code poetry is better.
    yup That's what i am trying to do.I have just included a private method in class B to be able to access the private member variable c.The code is quite sensitive that's why i do not want to post it

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Quote Originally Posted by sigur47
    I have just included a private method in class B to be able to access the private member variable c.
    That won't work: A cannot access the private members of B, whether they are member variables or member functions.

    Quote Originally Posted by sigur47
    The code is quite sensitive that's why i do not want to post it
    Then come up with the smallest and simplest program that demonstrates the error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by laserlight View Post
    That won't work: A cannot access the private members of B, whether they are member variables or member functions.


    Then come up with the smallest and simplest program that demonstrates the error.
    Would do so now thanks for your patience

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by laserlight View Post
    That won't work: A cannot access the private members of B, whether they are member variables or member functions.


    Then come up with the smallest and simplest program that demonstrates the error.
    i have posted a code up please kindly have a look at it

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    Use code tags, please.
    Also, the compiler is right to complain:
    Country is not supposed to know how a City is implemented. You should provide public methods in City to allow the addition Points, whatever they are.
    Last edited by Elysia; 01-29-2012 at 10:48 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by Elysia View Post
    Use code tags, please.
    Also, the compiler is right to complain:
    Country is not supposed to know how a City is implemented. You should provide public methods in City to allow the addition Points, whatever they are.
    I cannot add extra public method to my player object because its implementing an interface

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    Just because it is implementing an interface does not mean you cannot add more functions. Of course, the question is, does it make sense? This may or may not be.
    Regardless, the design you have now is bad, and cannot work. So either you add more functions to the class, augment the interface or make a new interface and implement it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    118
    Quote Originally Posted by Elysia View Post
    Just because it is implementing an interface does not mean you cannot add more functions. Of course, the question is, does it make sense? This may or may not be.
    Regardless, the design you have now is bad, and cannot work. So either you add more functions to the class, augment the interface or make a new interface and implement it.
    the interface cannot be changed because everyone working on the project has to have the same interface.i cant use private methods because it wont work.Would there be another way of designing my program

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,621
    OK, so there are two ways:
    - Make the variable public (which is a dumb way).
    - Make a public method that does what you want (which is a good way).

    You can implement the public method in several ways, obviously. It can be inherited from one or more interfaces, or it can be a method only existing in that specific class.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,749
    Quote Originally Posted by sigur47
    the interface cannot be changed because everyone working on the project has to have the same interface.
    Go back to your team and report that there is something deficient about the interface of B. Discuss and change the interface to add the functionality needed to access/modify C from A through B.

    Of course, if by implementing an interface to mean to say that you're responsible for designing and implementing C, but C must implement an interface specified by say, an abstract base class that has been agreed on to serve as a pure interface, then that is a different matter: you can freely add to the public interface of C as long as you are also implementing the interface specified.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 08:42 AM
  2. private class member access
    By DarkMasterBosel in forum C++ Programming
    Replies: 6
    Last Post: 03-30-2008, 11:37 AM
  3. Replies: 9
    Last Post: 05-03-2007, 03:50 PM
  4. Private class member not being set
    By cboard_member in forum C++ Programming
    Replies: 4
    Last Post: 04-06-2007, 11:38 AM
  5. Setting And Accessing Private Member Variables
    By bumfluff in forum C++ Programming
    Replies: 4
    Last Post: 04-12-2006, 06:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21