Static vector losing its contents

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2009
Posts: 22
Reputation: pac-man is an unknown quantity at this point 
Solved Threads: 0
pac-man pac-man is offline Offline
Newbie Poster

Static vector losing its contents

 
0
  #1
19 Days Ago
Hi guys,

I have a static vector which holds objects of a class. I want this vector to be singleton i.e. be the only vector of this type which will indefinitely store the relevant objects.

Unfortunately, a problem arises whereby, when I access the vector from a different class, for some reason it "loses" it's contents and is empty?
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,176
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 146
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster
 
0
  #2
19 Days Ago
>>a problem arises whereby, when I access the vector from a different class, for some reason it "loses" it's contents and is empty?
<<

From what content?

How about you make a adapter class, that wraps vector, and make that
adapter class singleton.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 22
Reputation: pac-man is an unknown quantity at this point 
Solved Threads: 0
pac-man pac-man is offline Offline
Newbie Poster
 
0
  #3
19 Days Ago
Hi there and thanks for the response. In the program, I navigate to an 'add book' GUI panel (A) which adds books to the vector. I then want to navigate to another panel (B) and populate a combobox using the contents of the vector. The vector remains populated in panel A but not B.

In short,
Home->Panel A->Home->Panel A the vector is fine
Home->Panel A->Home->Panel B then the vector appears empty

I tried to wrap the vector in a singleton class before trying to access the vBooks vector through the class as follows:

  1. class SingletonVector
  2. {
  3. public:
  4. static vector<Book> vBooks;
  5. SingletonVector() {};
  6.  
  7. };


  1. SingletonVector sv;
  2. Book b;
  3. b.setTitle("Test title");
  4. sv->vBooks.push_back(b); // PROBLEM LINE

The code compiles fine apart from when I use the function 'push_back' which results in the following error:

error LNK2001: unresolved external symbol "public: static class std::vector<class Book,class std::allocator<class Book> > SingletonVector::vBooks" (?vBooks@SingletonVector@@2V?$vector@VBook@@V?$allocator@VBook@@@std@@@std@@A)
vc_mswd\myapp.exe : fatal error LNK1120: 1 unresolved externals
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,176
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 146
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Veteran Poster
 
1
  #4
19 Days Ago
use SingletonVector::vBooks.push_back(b);
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 22
Reputation: pac-man is an unknown quantity at this point 
Solved Threads: 0
pac-man pac-man is offline Offline
Newbie Poster
 
0
  #5
19 Days Ago
Still no go..feels like the fix is not much further!

MainFrame.obj : error LNK2001: unresolved external symbol "public: static class std::vector<class Book,class std::allocator<class Book> > SingletonVector::vBooks" (?vBooks@SingletonVector@@2V?$vector@VBook@@V?$allocator@VBook@@@std@@@std@@A)
Reports.obj : error LNK2001: unresolved external symbol "public: static class std::vector<class Book,class std::allocator<class Book> > SingletonVector::vBooks" (?vBooks@SingletonVector@@2V?$vector@VBook@@V?$allocator@VBook@@@std@@@std@@A)
vc_mswd\myapp.exe : fatal error LNK1120: 1 unresolved externals
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 22
Reputation: pac-man is an unknown quantity at this point 
Solved Threads: 0
pac-man pac-man is offline Offline
Newbie Poster
 
0
  #6
19 Days Ago
I can only get rid of the linker errors by deleting any code that works on the vector i.e. push_back / .at

What am I missing..

Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 22
Reputation: pac-man is an unknown quantity at this point 
Solved Threads: 0
pac-man pac-man is offline Offline
Newbie Poster
 
0
  #7
19 Days Ago
Finally!

The problem was that I wasn't fully declaring the static variable outside the scope of the class before using it!

Thanks for the help fp
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC