943,946 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 943
  • C++ RSS
Nov 9th, 2009
0

Static vector losing its contents

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
pac-man is offline Offline
68 posts
since Oct 2009
Nov 9th, 2009
0
Re: Static vector losing its contents
>>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.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Nov 9th, 2009
0
Re: Static vector losing its contents
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:

C++ Syntax (Toggle Plain Text)
  1. class SingletonVector
  2. {
  3. public:
  4. static vector<Book> vBooks;
  5. SingletonVector() {};
  6.  
  7. };


C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
pac-man is offline Offline
68 posts
since Oct 2009
Nov 9th, 2009
1
Re: Static vector losing its contents
use SingletonVector::vBooks.push_back(b);
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Nov 9th, 2009
0
Re: Static vector losing its contents
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
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
pac-man is offline Offline
68 posts
since Oct 2009
Nov 9th, 2009
0
Re: Static vector losing its contents
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..

Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
pac-man is offline Offline
68 posts
since Oct 2009
Nov 9th, 2009
0
Re: Static vector losing its contents
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
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
pac-man is offline Offline
68 posts
since Oct 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Program wont work (Need help with parameter passing)
Next Thread in C++ Forum Timeline: Writing PC programs on a Palm





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC