Static vector losing its contents

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

Join Date: Oct 2009
Posts: 54
Reputation: pac-man is an unknown quantity at this point 
Solved Threads: 3
pac-man pac-man is offline Offline
Junior Poster in Training

Static vector losing its contents

 
0
  #1
Nov 9th, 2009
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,456
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso
 
0
  #2
Nov 9th, 2009
>>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.
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 54
Reputation: pac-man is an unknown quantity at this point 
Solved Threads: 3
pac-man pac-man is offline Offline
Junior Poster in Training
 
0
  #3
Nov 9th, 2009
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,456
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso
 
1
  #4
Nov 9th, 2009
use SingletonVector::vBooks.push_back(b);
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 54
Reputation: pac-man is an unknown quantity at this point 
Solved Threads: 3
pac-man pac-man is offline Offline
Junior Poster in Training
 
0
  #5
Nov 9th, 2009
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: 54
Reputation: pac-man is an unknown quantity at this point 
Solved Threads: 3
pac-man pac-man is offline Offline
Junior Poster in Training
 
0
  #6
Nov 9th, 2009
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: 54
Reputation: pac-man is an unknown quantity at this point 
Solved Threads: 3
pac-man pac-man is offline Offline
Junior Poster in Training
 
0
  #7
Nov 9th, 2009
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:




Views: 283 | Replies: 6
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC