I have a templated Stack class implemented internally with vector.
Here is the content of my (simplified) TStack.h:
#include <vector>
#include <iostream>
template<typename T> class TStack;
template<typename T> TStack<T> operator+(const TStack<T> &s1, const TStack<T> &s2);
template<typename T>
class TStack {
friend TStack<T> operator+<>(const TStack<T> &s1, const TStack<T> &s2);
private:
std::vector<T> items;
public:
void printAll() {
std::cout << "The content of the stack is: ";
typename std::vector<T>::iterator it;
for(it = items.begin(); it < items.end(); it++) {
std::cout << *it << " ";
}
std::cout << std::endl;
}
};
template<typename T>
TStack<T> operator+(const TStack<T> &s1, const TStack<T> &s2) {
TStack<T> result = s1;
typename std::vector<T>::iterator it;
//below is line 41
for(it = s2.items.begin(); it < s2.items.end(); it++) {
result.items.push_back(*it);
}
return result;
}
And this is my (simplified) main class:
#include <iostream>
#include "TStack.h"
using namespace std;
int main(int argc, char *argv[]) {
TStack<int> intStack;
intStack.push(4);
TStack<int> secondIntStack;
secondIntStack.push(10);
cout << "Addition result: " << endl;
//below is line 27
TStack<int> result = intStack + secondIntStack;
result.printAll();
return 0;
}
And this is the compilation result:
In file included from main.cpp:2:
TStack.h: In function ‘TStack<T> operator+(const TStack<T>&, const TStack<T>&) [with T = int]’:
main.cpp:27: instantiated from here
TStack.h:41: error: no match for ‘operator=’ in ‘it = s2->TStack<int>::items.std::vector<_Tp, _Alloc>::begin [with _Tp = int, _Alloc = std::allocator<int>]()’
/usr/include/c++/4.4/bits/stl_iterator.h:669: note: candidates are: __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >& __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >::operator=(const __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&)
make: *** [main.exe] Error 1
I have no idea what is the meaning of the error message.
In the operator+ function, I used the same way to get the iterator inside the printAll(), but it doesn’t work properly inside the operator+ function.
I know I can just avoid using the iterator in the operator+ function, but I am just curious on how to fix this.
Use
const_iteratorinstead ofiterator:Because
s1is a const object. Sos1.itemswill also be const object as well, which meanss1.items.begin()will returnconst_iterator, not non-constiterator.Better implementation of operator+()
You can improve the implementation of
operator+(). Instead of using a manual loop, andpush_backfunction, you can useinsertfunction as:It completely the avoids the problem of
iteratorwhich you face in your code.More better implementation of operator+()
If you accept the first argument by value, instead of const reference, then that is even better:
As the first argument is a copy itself, you don’t need to create a local variable called
resultexplicitly. You simply can adds2tos1and returns1.