Hi, i have a problem with a vector.

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

Code:

void LogView::init(){
	int a = 0;
	while (LogFile.good()){
		LogFile.getline(line, 6);
		moves.push_back(line);
	cout << "vector :"<< moves[a]<< endl; // here is OK
	a++;
	}
	moves.begin();
	cout << "vektor :"<< moves[1]<< endl;  // here is NOT OK
	cout << "vektor :"<< moves[2]<< endl;  // here is NOT OK
	cout << "vektor :"<< moves[3]<< endl;  // here is NOT OK
	cout << "vektor :"<< moves.at(1)<< endl;  // here is NOT OK
	cout << "vektor :"<< moves.at(2)<< endl;  // here is NOT OK
	cout << "vektor :"<< moves.at(3)<< endl;  // here is NOT OK
	cout << "vektor :"<< moves.front()<< endl;  // here is NOT OK
	cout << "vektor :"<< moves.back()<< endl;  // here is NOT OK 
	//AN ITERATION FOR IS NOT OK TOO.
	cout << moves.size(); // IT'S OK!
	}
}

Thank You.

How do you assing memory to each pointer?

Hi, in header:

private:
	std::ifstream LogFile;
	char line[5];
	std::vector<char*> moves;

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).

Thank You!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.