简体   繁体   English

如何在模板 class 上实现运算符重载?

[英]How to implement operator overloading on a template class?

I am having an issue trying to overload the addition operator on a template class for complex numbers.我在尝试为复数重载模板 class 上的加法运算符时遇到问题。

In my.h I have forward declaration for both the class and the operator function function, and I specifically overloaded operator+<T> rather than just operator+ so that it works for multiple types.在 my.h 中,我对 class 和运算符 function function 进行了前向声明,并且我特别重载了operator+<T> ,而不仅仅是operator+ ,因此它适用于多种类型。 It compiles fine when I only include the.h file, but as soon as I try to declare MyComplex<int> it gives some weird errors that I'm having difficulty deciphering.当我只包含 .h 文件时,它编译得很好,但是当我尝试声明MyComplex<int>时,它会给出一些我难以解读的奇怪错误。

my_complex.h: my_complex.h:

#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: my_complex.cpp:

#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:主.cpp:

#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;

}

It compiles fine when I only include the.h file in main file, but as soon as I try to declare MyComplex<int> it gives some errors that I am having difficulty deciphering.当我只在主文件中包含 .h 文件时,它编译得很好,但是一旦我尝试声明MyComplex<int>它就会给出一些我难以解读的错误。

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;
                            ^~~~~~~~~

You cannot define a templated method in a.cpp file, put it in the header and it should work.您不能在 a.cpp 文件中定义模板化方法,将其放在 header 中,它应该可以工作。 See https://isocpp.org/wiki/faq/templates#templates-defn-vs-declhttps://isocpp.org/wiki/faq/templates#templates-defn-vs-decl

Furthermore, this此外,这

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);

so you should add <T>所以你应该添加<T>

This actually does not explain your error, but the error message does not fit the files you gave, the.hpp has not 36 lines but that's where the error is, so debugging this is quite hard.这实际上并不能解释您的错误,但是错误消息与您提供的文件不符,.hpp 没有 36 行,但这就是错误所在,因此调试起来非常困难。

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

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