By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,305 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,305 IT Pros & Developers. It's quick & easy.

Help with using class as vector

P: 12
Hi all,

I am trying to use the class as a vector and pass parameters to the class. In loop.run(), I do certain things, then I would like to delete all the elements of the class.

Expand|Select|Wrap|Line Numbers
  1. 1  class  SampleMarketDataClient
  2. 2  {
  3. 3       ....
  4. 4  };
  5. 5  int main()
  6. 6  {
  7. 7      vector<SampleMarketDataClient> clients;
  8. 8      while (!cin.eof())
  9. 9      {
  10. 10             if(cin.good())
  11. 11             {
  12. 12                    cin >> lineString;
  13. 13                     // Set up a market data subscription.
  14. 14                    clients.push_back( SampleMarketDataClient(lineString) );
  15. 15                    clients++;
  16. 16              }
  17. 17    }
  18. 18    loop.run();
  19. 19    clients.clear();
  20. 20 }
But this doesn't work. I think the line numbers 14, 15 & 19 are not the proper way to use the class as vectors. When I compile the program, I get errors. Is there a better way to do it? If so please let me know. Thanks a lot in advance.
Feb 21 '07 #1
Share this Question
Share on Google+
2 Replies


Ganon11
Expert 2.5K+
P: 3,652
For line 14, I suggest separating this into two different lines. First, create an object, and then push_back it onto the vector. Line 15 is unnecessary and will probably produce one of your errors - there is no need to ++ the vector, and there is no ++ operator on vectors.

Line 19 seems just fine to me, so I'd say you're OK.

Try changing line 14 as I suggested, and then see if it works - if not, come back and post the errors you're receiving.
Feb 21 '07 #2

P: 12
Hi

Thanks a lot for your reply. If I do as I suggested, I dont get any error while compiling but when I run the program, it gives me an 'Segmentation fault'. Anything you can suggets.

Expand|Select|Wrap|Line Numbers
  1. vector<SampleMarketDataClient *> clients;
  2. SampleMarketDataClient *temp;
  3. while (!cin.eof())
  4. {
  5.       if(cin.good())
  6.       {
  7.         // Read in the next line of text from the file
  8.        cin >> lineString;
  9.     // Set up a market data subscription.
  10.     temp = new SampleMarketDataClient(data, loop, lineString);
  11.     clients.push_back( temp );
  12.       }
  13. }
Cheers
Feb 21 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.