operator= implementation

Heyy
I have implemented operator= but obviously i need another function for the iter because i get this error Error 1 error C2440: 'initializing' : cannot convert from 'int' to 'std::_List_iterator<_Mylist>' c:\users\hebrew\documents\visual studio 2010\projects\test\test\system.cpp 67 when i use the SearchBrand function:

 
  list<Brand>::iterator i=SearchBrand(BrandName);


How can i implement an operator= function that can work for (iter i=func()) ? what is the implementation?
class std::list<T>::iterator is already defined. You may not to add for this class your own assignment operator.
so what does this error means?
SearchBrand needs to return a std::list<Brand>::iterator instead of an int.
Last edited on
It gives me now the two errors: Error 1 error C2440: 'initializing' : cannot convert from 'Brand' to 'std::_List_iterator<_Mylist>' c:\users\hebrew\documents\visual studio 2010\projects\test\test\system.cpp 64


2 IntelliSense: no suitable user-defined conversion from "Brand" to "std::_List_iterator<std::_List_val<Brand, std::allocator<Brand>>>" exists c:\users\hebrew\documents\visual studio 2010\projects\test\test\system.cpp 64

1
2
3
4
5
6
7
8
9
Brand System::SearchBrand(string BrandName) {
	list<Brand>::iterator findbrand;

	for(findbrand=Brands.begin(); findbrand!=Brands.end(); ++findbrand){
		if (findbrand->getBrandName() == BrandName)
		return *findbrand;
	}
		
}
Here you have an iterator on your left. This is a container which, when dereference becomes an object, but it also has links to the other objects in the list.

On the right of your operator is a function which returns an int. An int can't be used like an iterator. It's incompatible.

You'll either need to:
1) Change SearchBrand() to return an iterator.
2) Find a suitable method to convert from the int to an iterator.

Assuming BrandName is a list<Brand>, your search function may look something like this:

1
2
3
4
5
6
list<Brand>::iterator SearchBrand(list<Brand>& bn)
{
    for (list<Brand>::iterator it = br.begin(); it != br.end(); ++it)
        if (bn->name = "Pick Me!")
            return it;
}


If the integer is supposed to represent the index of the object in the list try this:

1
2
int i = SearchBrand(BrandName);
SomeBrandList.at(i);


Note that to use that code snippet, you'll need to use std::vector instead of std::list.

Topic archived. No new replies allowed.