vector <char*>

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 9
Reputation: luisvega is an unknown quantity at this point 
Solved Threads: 0
luisvega luisvega is offline Offline
Newbie Poster

vector <char*>

 
0
  #1
Oct 5th, 2008
Hi, i have a problem with a vector.

I declare a vector<char*> moves; in header.

Code:
  1. void LogView::init(){
  2. int a = 0;
  3. while (LogFile.good()){
  4. LogFile.getline(line, 6);
  5. moves.push_back(line);
  6. cout << "vector :"<< moves[a]<< endl; // here is OK
  7. a++;
  8. }
  9. moves.begin();
  10. cout << "vektor :"<< moves[1]<< endl; // here is NOT OK
  11. cout << "vektor :"<< moves[2]<< endl; // here is NOT OK
  12. cout << "vektor :"<< moves[3]<< endl; // here is NOT OK
  13. cout << "vektor :"<< moves.at(1)<< endl; // here is NOT OK
  14. cout << "vektor :"<< moves.at(2)<< endl; // here is NOT OK
  15. cout << "vektor :"<< moves.at(3)<< endl; // here is NOT OK
  16. cout << "vektor :"<< moves.front()<< endl; // here is NOT OK
  17. cout << "vektor :"<< moves.back()<< endl; // here is NOT OK
  18. //AN ITERATION FOR IS NOT OK TOO.
  19. cout << moves.size(); // IT'S OK!
  20. }
  21. }

Thank You.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 273
Reputation: Sci@phy will become famous soon enough Sci@phy will become famous soon enough 
Solved Threads: 42
Sci@phy's Avatar
Sci@phy Sci@phy is offline Offline
Posting Whiz in Training

Re: vector <char*>

 
0
  #2
Oct 5th, 2008
How do you assing memory to each pointer?
Last edited by Sci@phy; Oct 5th, 2008 at 11:20 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: luisvega is an unknown quantity at this point 
Solved Threads: 0
luisvega luisvega is offline Offline
Newbie Poster

Re: vector <char*>

 
0
  #3
Oct 5th, 2008
Hi, in header:

  1. private:
  2. std::ifstream LogFile;
  3. char line[5];
  4. std::vector<char*> moves;
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: vector <char*>

 
0
  #4
Oct 5th, 2008
When you append pointers onto a vector of char, the pointer is copied. You are expecting the data the pointer points at to be copied.

With a vector<char *> you need to explicitly copy the data as well as the pointer.

An easier way would be to use a std::vector<std::string> instead (as std::string objects copy the data implicitly).
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: luisvega is an unknown quantity at this point 
Solved Threads: 0
luisvega luisvega is offline Offline
Newbie Poster

Re: vector <char*>

 
0
  #5
Oct 5th, 2008
Thank You!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC