Hi,

I'm doing a program that has a class called Liga. That class has a vector with 'Equipa's.

Liga has a vector called 'pessoas' which I want to access inside a function in each Equipa, is this possible?

for example:

Liga.h

...
vector<Pessoa*> pessoas;
...

Equipa.cpp

...
void Equipa::mostraStaff()
{
[INDENT]for(unsigned int i=0;i!=pessoas.size();i++)
[INDENT]pessoas[i].mostra();[/INDENT][/INDENT]
}
...

thanks in advance!

Recommended Answers

All 5 Replies

Well you have a few options.
1. You can make pessoas public
2. You can create a function called getPessoas() which will return a pointer to pessoas for that particular Liga object.
3. You can make Equipa a friend of Liga, that way it can access its data members.

You can make pessoas public.

that's good! I just need to declare it as public and then include "Liga.h" in "Equipa.h", right?

as for declaring Equipa a friend of Liga, how can I do that?

thanks again!

Yes you can make it public and add the header to Equipa. If you want only Equipa to access the data members of Liga, and not any other class, then you can declare it as a friend:

class Liga {
  friend class Equipa;
....
};

>that's good! I just need to declare it as public
It's rather depressing how you latched on to the worst possible option that stilllearning suggested.

>that's good! I just need to declare it as public
It's rather depressing how you latched on to the worst possible option that stilllearning suggested.

actually I didn't :P
I modified my primary algorithm and now I don't need to do that anymore :)

thanks for all your help, though!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.