Accesing private vector member in another classes

Hey i have big problem , maybe it's easy to fix but really idk how. I'm creating game and i have one class named TMap. Here i have private vector of SCharacter structures for player info. I need to modify this vector in all other classes (normal for game) so i tried to do vector reference to vector in TMap class something like this in class named TCharacter :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

class TMap
{
std::vector<SCharacter> characters;
public:
//constructor and other things here
std::vector<SCharacter>& GetCharacter(){return characters;}
};

class TCharacter
{
const std::shared_ptr<TMap> Map;
std::vector<SCharacter> &characters;
public:
TCharacter(const std::shared_ptr<TMap> Map) : Map(Map),characters(Map->GetCharacter()){}
};


But it isn't working i got errors that characters in TCharacter class cannot be initialized by value . I tried also making vector in TServer class , pass it by reference and initialize it in TCharacter class but it isn't working too.Values aren't the same it's creating new vector gor those classes even if it's reference to othet vector. How i can fix my problem ??
Last edited on
GetCharacter should be a Method so add the braces()
std::vector<SCharacter>& GetCharacter() {return characters;}

don't forget to use it as a method, also use braces here:
TCharacter(const std::shared_ptr<TMap> Map) : Map(Map),characters(Map->GetCharacter()){}
Last edited on
I just wrote it without seeing the code. I have brackets ;).
you pass a const shared_ptr and assign a non-const shared_ptr to it
try dropping the const

TCharacter(const std::shared_ptr<TMap> Map)
If you want full control over vector in other classes try: http://en.wikipedia.org/wiki/Friend_class
But wouldn't it be bad if i have example 10 classes which are accessing this vector and every class is friend?
But wouldn't it be bad if i have example 10 classes which are accessing this vector and every class is friend?

yeah, well, with your solution everyone can access the vector...
One question, you want to modify it in many other classes, why private?
Topic archived. No new replies allowed.