First, I apologize for any concept errors about C++. I am pretty new.

I am having trouble iterating through each node of a passed linked list. What I have right now is an implementation file with a few functions. The class is called listClass and has a private member function of

list<coordStruct> coordinates;

. A standard stucture has also been set up called coordStruct that has two elements, x and y.

It appears that my problem is how I use the passed-in class. I have it as passedIn->coordinates.begin() and it gives an error. When using Visual Studio and intellisense, it shows that passedIn-> has no member functions, but passedIn. does and it has the coordinates associated. So I tried using passedIn.coordinates.begin() but I get a different error.

I have summarized my problem with the two snippets of code below:

this works:

listClass& listClass::printCoords() 
{
	list<coordStruct>::iterator i;
	for (i = this->coordinates.begin(); i != this->coordinates.end(); i++) {
		cout << "x: " << (*i).x << ", y: " << (*i).y) << endl;
	}
}

This doesn't work. It gives an error of error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::list<_Ty>::_Const_iterator<_Secure_validation>' (or there is no acceptable conversion). It points to the start of the for loop

listClass& listClass::operator=(const listClass &passedIn) 
{
	this->coordinates.clear();
	
	list<coordStruct>::iterator i;
	for (i = passedIn->coordinates.begin(); i != passedIn->coordinates.end(); i++) {
		this->insertNode((*i).x, (*i).y);
	}
}

You pass a const listClass reference to operator=, so try a const_iterator. Moreover your op= is lacking the return statement. And by the way, do you know std::copy and std::back_inserter? This reduces op= to:

listClass& listClass::operator=(const listClass &passedIn) 
{
    coordinates.clear();
    std::copy(passedIn.begin(), passedIn.end(), std::back_inserter(coordinates));
    return *this;
}

You pass a const listClass reference to operator=, so try a const_iterator. Moreover your op= is lacking the return statement. And by the way, do you know std::copy and std::back_inserter? This reduces op= to:

listClass& listClass::operator=(const listClass &passedIn) 
{
    coordinates.clear();
    std::copy(passedIn.begin(), passedIn.end(), std::back_inserter(coordinates));
    return *this;
}

Thank you. I had to change the line std::copy(passedIn.begin(), passedIn.end(), std::back_inserter(coordinates)); to std::copy(passedIn.begin(), passedIn.end(), std::back_inserter(coordinates)); in order for it to work.

I also tried taking off const and that fixes my other problems too. For example, I can iterate through a list by doing:
listClass& listClass::operator=(listClass &passedIn)

{
	coordinates.clear();

	list<coordinates>::iterator i;
	for(i = passedIn.coordinates.begin(); i != passedIn.coordinates.end(); i++) {
		cout << (*i).x<< "," << (*i).y<< endl;
	}
    return *this;
}

I am not saying that code works as a copy constructor. It was just a test to iterate through a passed in list.

Is it bad to not make the passed in variable a const?

Anyways, thanks again for your help!

Thank you. I had to change the line std::copy(passedIn.begin(), passedIn.end(), std::back_inserter(coordinates)); to std::copy(passedIn.begin(), passedIn.end(), std::back_inserter(coordinates)); in order for it to work.

Hmmm, seeing no difference here......

Is it bad to not make the passed in variable a const?

It's just bad coding style. op= makes no changes to passedIn, so why not notify the user of op= that the passed object remains unchanged? In certain cases the compiler can make code optimizations, if he knows of the constness of an object.

Anyways, thanks again for your help!

np, you're welcome.

This article has been dead for over six months. Start a new discussion instead.