开发者

C++ operator overloading on templated class

开发者 https://www.devze.com 2023-04-10 18:21 出处:网络
I have a templated Stack class implemented internally with vector. Here is the content of my (simplified) TStack.h:

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.i开发者_运维知识库tems.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_iterator instead of iterator:

typename std::vector<T>::const_iterator it;

Because s1 is a const object. So s1.items will also be const object as well, which means s1.items.begin() will return const_iterator, not non-const iterator .


Better implementation of operator+()

You can improve the implementation ofoperator+(). Instead of using a manual loop, and push_back function, you can use insert function as:

template<typename T>
TStack<T> operator+(const TStack<T> &s1, const TStack<T> &s2) {
    TStack<T> result(s1); //use direct copy-initialization
    result.insert(result.end(), s2.begin(), s2.end());
    return result;
}

It completely the avoids the problem of iterator which 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:

template<typename T>
TStack<T> operator+(TStack<T> s1, const TStack<T> &s2) {
    s1.insert(s1.end(), s2.begin(), s2.end()); //s1 is a copy, after all!
    return s1; 
}

As the first argument is a copy itself, you don't need to create a local variable called result explicitly. You simply can add s2 to s1 and return s1.


you cannot assign a const iterator (s2.items.begin()) to a non const iterator. Use

typename std::vector<T>::const_iterator it;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号