0

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

3
Contributors
8
Replies
9
Views
5 Years
Discussion Span
Last Post by Ancient Dragon
Featured Replies
  • make the vector static. [code] class Players{ public: [color=red]static [/color]vector <strings> info }; [/code] Then in one of the *.cpp files declare it again as if it were a global variable [code] // Players.cpp #include "Players.h" Players::vector<string> info; [/code] Read More

  • Correction: [icode]vector<string> Players::info;[/icode] Read More

  • [URL="http://www.codeproject.com/Articles/18314/Static-Initialization-Function-in-Classes"]Here is[/URL] another example. I tried it and it works. Read More

1

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;

Edited by Ancient Dragon: n/a

Comments
Direct / wise
0

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");}
0

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)?

Edited by Ancient Dragon: n/a

0

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

Edited by Despairy: n/a

0

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

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.