首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >如何在模板类上实现操作符重载?

如何在模板类上实现操作符重载?
EN

Stack Overflow用户
提问于 2019-11-10 07:02:34
回答 1查看 52关注 0票数 0

我在尝试重载复数模板类上的加法运算符时遇到了一个问题。

在我的.h中,我对类和运算符函数函数都进行了正向声明,并且我特别重载了operator+<T>,而不仅仅是operator+,这样它就可以处理多种类型。当我只包含.h文件时,它编译得很好,但只要我试图声明MyComplex<int>,它就会给出一些奇怪的错误,我很难破译这些错误。

my_complex.h:

代码语言:javascript
代码运行次数:0
运行
复制
#ifndef _MY_COMPLEX_H
#define _MY_COMPLEX_H

using namespace std;

//forward declarations
template<typename T> class MyComplex;

template<typename T> MyComplex<T> operator+(const MyComplex<T>& cmplx1, const MyComplex<T>& cmplx2);

template<typename T>
class MyComplex{
        private:
                T _real;
                T _imaginary;
        public:
                MyComplex();
                MyComplex(T real, T img);
                T getReal();
                T getImaginary();
                void setReal(T real);
                void setImaginary(T img);

                friend MyComplex operator+<T>(const MyComplex& cmplx1, const MyComplex& cmplx2);
}
#endif

my_complex.cpp:

代码语言:javascript
代码运行次数:0
运行
复制
#include "my_complex.h"
...
template <typename T>
MyComplex<T> operator+(const MyComplex<T>& cmplx1, const MyComplex<T>& cmplx2){
        MyComplex<T> temp;
        temp._real = cmplx1._real + cmplx2._real;
        temp._imaginary = cmplx1._imaginary + cmplx2._imaginary;

        return temp;
}
...

main.cpp:

代码语言:javascript
代码运行次数:0
运行
复制
#include "my_complex.h"

int main(int argc, char* argv[]){
        MyComplex<int> cmplx1 = MyComplex<int>(3, 5);//error is here
        MyComplex<int> cmplx2 = MyComplex<int>(5, 7);

        cmplx1 = cmplx1 + cmplx2;

        return 0;

}

当我只在主文件中包含.h文件时,它编译得很好,但只要我试图声明MyComplex<int>,它就会给出一些我很难破译的错误。

代码语言:javascript
代码运行次数:0
运行
复制
In file included from /usr/include/c++/7/bits/stl_algobase.h:67:0,
                     from /usr/include/c++/7/bits/char_traits.h:39,
                     from /usr/include/c++/7/ios:40,
                     from /usr/include/c++/7/ostream:38,
                     from /usr/include/c++/7/iostream:39,
                     from main.cpp:1:
    /usr/include/c++/7/bits/stl_iterator.h: In instantiation of ‘class std::reverse_iterator<int>’:
    /usr/include/c++/7/bits/stl_iterator.h:400:5:   required by substitution of ‘template<class _Iterator> std::reverse_iterator<_Iterator> std::operator+(typename std::reverse_iterator<_Iterator>::difference_type, const std::reverse_iterator<_Iterator>&) [with _Iterator = int]’
    my_complex.h:36:20:   required from ‘class MyComplex<int>’
    main.cpp:8:17:   required from here
    /usr/include/c++/7/bits/stl_iterator.h:101:11: error: no type named ‘iterator_category’ in ‘struct std::iterator_traits<int>’
         class reverse_iterator
               ^~~~~~~~~~~~~~~~
    /usr/include/c++/7/bits/stl_iterator.h:115:55: error: no type named ‘difference_type’ in ‘struct std::iterator_traits<int>’
           typedef typename __traits_type::difference_type difference_type;
                                                           ^~~~~~~~~~~~~~~
    /usr/include/c++/7/bits/stl_iterator.h:116:48: error: no type named ‘pointer’ in ‘struct std::iterator_traits<int>’
           typedef typename __traits_type::pointer  pointer;
                                                    ^~~~~~~
    /usr/include/c++/7/bits/stl_iterator.h:117:50: error: no type named ‘reference’ in ‘struct std::iterator_traits<int>’
           typedef typename __traits_type::reference  reference;
                                                      ^~~~~~~~~
    /usr/include/c++/7/bits/stl_iterator.h: In instantiation of ‘class std::move_iterator<int>’:
    /usr/include/c++/7/bits/stl_iterator.h:1201:5:   required by substitution of ‘template<class _Iterator> std::move_iterator<_IteratorL> std::operator+(typename std::move_iterator<_IteratorL>::difference_type, const std::move_iterator<_IteratorL>&) [with _Iterator = int]’
    my_complex.h:36:20:   required from ‘class MyComplex<int>’
    main.cpp:8:17:   required from here
    /usr/include/c++/7/bits/stl_iterator.h:1019:50: error: no type named ‘reference’ in ‘struct std::iterator_traits<int>’
           typedef typename __traits_type::reference  __base_ref;
                                                      ^~~~~~~~~~
    /usr/include/c++/7/bits/stl_iterator.h:1023:57: error: no type named ‘iterator_category’ in ‘struct std::iterator_traits<int>’
           typedef typename __traits_type::iterator_category iterator_category;
                                                             ^~~~~~~~~~~~~~~~~
    /usr/include/c++/7/bits/stl_iterator.h:1024:52: error: no type named ‘value_type’ in ‘struct std::iterator_traits<int>’
           typedef typename __traits_type::value_type   value_type;
                                                        ^~~~~~~~~~
    /usr/include/c++/7/bits/stl_iterator.h:1025:55: error: no type named ‘difference_type’ in ‘struct std::iterator_traits<int>’
           typedef typename __traits_type::difference_type difference_type;
                                                           ^~~~~~~~~~~~~~~
    /usr/include/c++/7/bits/stl_iterator.h:1032:24: error: no type named ‘reference’ in ‘struct std::iterator_traits<int>’
         __base_ref>::type  reference;
                            ^~~~~~~~~
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-10 07:05:45

你不能在一个.cpp文件中定义一个模板化的方法,把它放在头文件中,它应该可以工作。请参阅https://isocpp.org/wiki/faq/templates#templates-defn-vs-decl

此外,这

代码语言:javascript
代码运行次数:0
运行
复制
friend MyComplex operator+<T>(const MyComplex& cmplx1, const MyComplex& cmplx2);
// should match the forward declaration
friend MyComplex<T> operator+<>(const MyComplex<T>& cmplx1, const MyComplex<T>& cmplx2);

因此您应该添加<T>

这实际上不能解释你的错误,但是错误消息不适合你给出的文件,.hpp没有36行,但这就是错误所在,所以调试相当困难。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58784315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档