acess private members on diferent classes

This is a discussion on acess private members on diferent classes within the C++ Programming forums, part of the General Programming Boards category; So guys I'm making a computer game in university.. I'm having a trouble to make this working.. I have to ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    3

    acess private members on diferent classes

    So guys I'm making a computer game in university.. I'm having a trouble to make this working.. I have to make a game similar to Civilization, I've done some classes like this:

    board - faction (vector) - cities - habitants
    - artifacts (vector)

    Basicaly my board includes some factions and artifacts.. My factions include some cities and habitants and then I've habitants outside the cities.. So I've done this..

    That's my board:
    Code:
    class tabuleiro
    {
    	Consola c;          // predefined to acess console functions
    	vector <faccao> f;     // factions
    	vector <artefacto> a; // artifacts
    
    	int posx, posy;
    	int max_ox, max_oy;
    	int fact_num;
    	int dificuldade;
    	string tipo;
    
    public:
    [....]
    my factions
    Code:
    class faccao
    {
    	string nome;
    	int ouro, comida, id, cor;
    
    	vector <cidade> city;        // cities
    	vector <habitante> hab;    // habitants
    public:
    [...]
    city
    Code:
    class cidade
    {
    	string nome;
    	int posx, posy, id, cor;
    
    public:
    	cidade(string nome2, int x, int y, int id2, int cor2); // constructor
    	cidade(void);
    	~cidade(void);
    [...]
    In the begining of the game we have to create a faction.. That's done, and then assossiate a city to it.

    Code:
    cidade cidad(f[i].get_nome(), posx+posx+2, posy+posy+2, i+1, 13); // Construtor
    f[i].city.push_back(cidad); // The problem is here**
    **Since I cannot use friend classes nor use public data how should I proceed?

    Thanks in advance,
    George

  2. #2
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    671
    Use Member functions. I can't see any in your code.

    Besides, your coding style is just awful.
    I never put signature, but I decided to make an exception.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,266
    Don't use (void) in C++. Just use empty brackets i.e. ()

    You might also want to read up on the Law of Demeter
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Thanks kmdv and iMalc.. I know that my coding style isn't that great but I do have member functions liket GET and SET in all classes.. Thought the only one I'm not seing is how do I get vector city function (class faccao) so that the function would be seen as f[i].GET_city() (class tabuleiro)...

    Thank you for your attention.

    EDIT:

    I do have declared in class faccao now
    Code:
    public:
    	vector <cidade> city;		// ILEGAL!!! Alterar depois para private
    	vector <habitante> hab;	// ILEGAL!!! Alterar depois para private
    so I can use f[i].city[i].. But since this is public I cannot use that.
    Last edited by JOKAZ; 12-11-2011 at 02:05 AM.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,641
    Right, you grant access to private members via accessors and mutators. You need to provide functions which add to the contained objects in your classes, like

    Code:
    city.addNewCity(const cidade &newCity)
    Also, I would say that your contained classes should be plural to reflect that there may be more than one, so

    Code:
    class faccao
    {
    	string nome;
    	int ouro, comida, id, cor;
    
    	vector <cidade> cities;        // cities
    	vector <habitante> habs;    // habitants
    public:
            void addCity(const cidade &city) { cities.push_back(city); }
            cidade getCity(const string &name) { /* implement search via name */ }
    [...]

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    3
    Thanks rags_to_riches that's what I was missing.. It did the trick. Thanks man

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. use of private members
    By MK27 in forum C++ Programming
    Replies: 15
    Last Post: 02-23-2010, 11:12 AM
  2. Accessing private Data members
    By Emeighty in forum C++ Programming
    Replies: 17
    Last Post: 08-14-2008, 11:16 PM
  3. Getting address of private members?
    By Aramil in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2006, 06:32 PM
  4. Private Static class members
    By earth_angel in forum C++ Programming
    Replies: 13
    Last Post: 08-29-2005, 06:37 AM
  5. Not able to access private data members
    By smitsky in forum C++ Programming
    Replies: 31
    Last Post: 05-09-2004, 07:06 PM

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