模板化类上的C++运算符重载

Her*_*ery 0 c++ templates operator-overloading friend-function

我有一个模板化的Stack类,内部使用vector实现.

这是我(简化)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;
}
Run Code Online (Sandbox Code Playgroud)

这是我的(简化)主类:

#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;
}
Run Code Online (Sandbox Code Playgroud)

这是编译结果:

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
Run Code Online (Sandbox Code Playgroud)

我不知道错误信息的含义是什么.

在operator +函数中,我使用相同的方法在printAll()中获取迭代器,但它在operator +函数内部无法正常工作.我知道我可以避免在operator +函数中使用迭代器,但我只是想知道如何解决这个问题.

Naw*_*waz 5

使用const_iterator而不是iterator:

typename std::vector<T>::const_iterator it;
Run Code Online (Sandbox Code Playgroud)

因为s1是一个const对象.所以s1.items也将是const对象,这意味着s1.items.begin()将返回const_iterator,而不是非const iterator.


更好地实现operator +()

你可以改进实施operator+().您可以使用以下函数来代替使用手动循环和push_back函数insert:

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;
}
Run Code Online (Sandbox Code Playgroud)

它完全避免了iterator您在代码中遇到的问题.


更好的实现operator +()

如果你接受第一个参数的值,而不是const引用,那就更好了:

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; 
}
Run Code Online (Sandbox Code Playgroud)

由于第一个参数是副本本身,因此您无需创建result显式调用的局部变量.您只需可以添加s2s1并返回s1.