Class members accessing problems

This is a discussion on Class members accessing problems within the C++ Programming forums, part of the General Programming Boards category; I have this function: Code: void Estacao::mostraInformacaoEstacao() { cout << "++ Estacao " << nome << endl; cout << " ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    9

    Class members accessing problems

    I have this function:

    Code:
    void Estacao::mostraInformacaoEstacao()
    {
        cout << "++ Estacao " << nome << endl;
        cout << "   tem ligacoes para:" << endl;
    
        //percorre as ligações existentes
        for (int i = 0; i < ligacoes.size(); i++)
        {
            //mostra a estação para a qual tem ligação
            cout << "     - " << ligacoes[i] << endl;
    
            for (int j = 0; j < linhas.size(); j++)
            {
                if (linhas[j].estacoes[0] == nome && linhas[j].estacoes[1] == ligacoes[i])
                {
                    cout << "(" << linhas.distancia << ", " << linhas.tipo << ")";
                }
            }
        }
    }
    that's defined inside estacao.cpp, that includes Estacao.h and Sistema.h, where the class Estacao is defined.

    My problem is that linhas (in the second "for" loop) is not defined inside the class Estacao, but in the class Sistema (defined in Sistema.h).

    I get the error that linhas is undeclared.

    It's not a problem of includes because I included every single header file used.

    Sorry if this has been posted before but i'm in a hurry.

    Thanks in advance.

  2. #2
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Does your definition of Estacao look something like this:

    Code:
    #include "Sistema.h"
    
    class Estacao : public Sistema
    {
    // class code
    };
    If not, then you can't access members of Sistema from Estacao. If you want to do this you have two immediately obvious options:

    1) Inherit Sistema in Estacao (as displayed above)
    2) you need to create a Sistema object inside of Estacao. Ex:
    Code:
    #include "Sistema.h"
    
    class Estacao
    {
    // Everything else
    private:
      Sistema sistema;
    // class code
    };
    Then you can do:
    Code:
    sistema.linhas.size()
    Assuming that linhas is public in Sistema. If it's not public I'm fairly sure you can figure out how to create a function to grab it.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    When i try the inheritance i get the following error:

    [CODE]
    C:\[...]\Estacao.h expected class-name before '{' token

    I never used inheritance before, so i don't knoe what might me causing this.

  4. #4
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    Did you #include the header file of the other class as shown in the example?

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    Quote Originally Posted by jverkoey
    Did you #include the header file of the other class as shown in the example?
    Yes.

  6. #6
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,903
    I can't see any obvious problem, so I'd say your best bet is to just create a Sistema member in your Estacao class and use that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking error with static template class members
    By cunnus88 in forum C++ Programming
    Replies: 6
    Last Post: 04-02-2009, 12:31 PM
  2. accessing class variables
    By pug in forum C# Programming
    Replies: 3
    Last Post: 05-20-2005, 08:46 AM
  3. Problems with static const class members
    By sigfriedmcwild in forum C++ Programming
    Replies: 5
    Last Post: 12-05-2004, 06:57 AM
  4. Class with members as classes
    By justdoit22 in forum C++ Programming
    Replies: 8
    Last Post: 12-29-2003, 05:58 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

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