简体   繁体   English

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

[英]C++ operator overloading on templated class

I have a templated Stack class implemented internally with vector. 我有一个内部使用vector实现的模板化Stack类。

Here is the content of my (simplified) TStack.h: 这是我的(简化的)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. 在operator +函数中,我使用相同的方法将迭代器放入printAll()内,但在operator +函数内无法正常工作。 I know I can just avoid using the iterator in the operator+ function, but I am just curious on how to fix this. 我知道我可以避免在operator +函数中使用迭代器,但是我很好奇如何解决此问题。

Use const_iterator instead of iterator : 使用const_iterator而不是iterator

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

Because s1 is a const object. 因为s1是const对象。 So s1.items will also be const object as well, which means s1.items.begin() will return const_iterator , not non-const iterator . 因此s1.items也将是const对象,这意味着s1.items.begin()将返回const_iterator ,而不是非const iterator


Better implementation of operator+() 更好地实现operator +()

You can improve the implementation of operator+() . 您可以改善operator+() Instead of using a manual loop, and push_back function, you can use insert function as: 除了使用手动循环和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;
}

It completely the avoids the problem of iterator which you face in your code. 完全避免了代码中遇到的iterator问题。


More better implementation of operator+() 更好地实现operator +()

If you accept the first argument by value, instead of const reference, then that is even better: 如果您按值接受第一个参数,而不是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; 
}

As the first argument is a copy itself, you don't need to create a local variable called result explicitly. 由于第一个参数是副本本身,因此您无需显式创建一个名为result的局部变量。 You simply can add s2 to s1 and return s1 . 您只需将s2添加到s1并返回s1

you cannot assign a const iterator ( s2.items.begin() ) to a non const iterator. 您不能将const迭代器( s2.items.begin() )分配给非const迭代器。 Use 采用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM