943,719 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 11225
  • C++ RSS
Oct 5th, 2008
0

vector <char*>

Expand Post »
Hi, i have a problem with a vector.

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

Code:
C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
luisvega is offline Offline
9 posts
since Oct 2008
Oct 5th, 2008
0

Re: vector <char*>

How do you assing memory to each pointer?
Last edited by Sci@phy; Oct 5th, 2008 at 11:20 am.
Reputation Points: 110
Solved Threads: 43
Posting Whiz in Training
Sci@phy is offline Offline
279 posts
since Sep 2008
Oct 5th, 2008
0

Re: vector <char*>

Hi, in header:

C++ Syntax (Toggle Plain Text)
  1. private:
  2. std::ifstream LogFile;
  3. char line[5];
  4. std::vector<char*> moves;
Reputation Points: 10
Solved Threads: 0
Newbie Poster
luisvega is offline Offline
9 posts
since Oct 2008
Oct 5th, 2008
0

Re: vector <char*>

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).
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Oct 5th, 2008
0

Re: vector <char*>

Thank You!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
luisvega is offline Offline
9 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: C++ Newbie
Next Thread in C++ Forum Timeline: Matrix





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


Follow us on Twitter


© 2011 DaniWeb® LLC