Hi, lets say i have

class Players{
public: vector <strings> info
};

and class that inherits from it

class son:public Players{

};

now in the main function i make 2 of those so now i have 2 vectors in memory
that vector holds the same data for all of the sons i create
so is there a simple way to make them share that info?

something like static? thanks

Recommended Answers

All 8 Replies

make the vector static.

class Players{
public: static vector <strings> info
};

Then in one of the *.cpp files declare it again as if it were a global variable

// Players.cpp
#include "Players.h"

Players::vector<string> info;
commented: Direct / wise +11

yeah i said like static cuz i wasnt sure it works
i get

error LNK2001: unresolved external symbol

any solution?
my player has a c-tor that uses the static.
and its a virtual class
ty

Player::Player{ 
info.pushback("name");}

Depends on what the external symbol was. Did you see the second part of my answer, or does that error refer to something else (the error message should have told you the name of the symbol)?

well , just saw the edit and i tried adding that line

Players::vector <string> info;

but i get "info is not a member of players"
so i tried just writing

Players::info

it worked but i still get ex-symbol problems
kinda long ... no idea why
(moveable = player)
(info=blockers)

1>Moveable.obj : error LNK2001: unresolved external symbol "protected: static class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > Moveable::_blockers" (?_blockers@Moveable@@1V?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@A)

ty for trying

Correction: vector<string> Players::info;

Here is another example. I tried it and it works.

Thanks! fixed!
so now all sons have the same info list right?
so if in son #1 i add another information everyone will get it?
thanks again

You are right. You might have to synchronize access to info if you have a multi-threaded program and two or more threads try to access info at the same time. You don't have to worry about that if your program has only one thread.

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.