c++ - 模板化类然后重载运算符 (C++)

标签 c++ class templates syntax overloading

下面的代码不工作,我找不到原因,任何帮助将不胜感激。

//In Maths.h file

template <class T> class Maths{
public:
    Maths<T>(T lhs);

    template<typename U>
    Maths<T>(const Maths<U>& otherMaths);

    ~Maths();

    template <typename U>
    Maths<T>& operator+(const Maths<U>& rhs);

    template <typename U> 
    Maths<T>& operator*(const Maths<U>& rhs);

    template <typename U> 
    Maths<T>& operator-(const Maths<U>& rhs);

private:
    T _lhs;
};



//In Maths.cpp file
#include "Maths.h"

template <class T>
Maths<T>::Maths(T lhs){
    _lhs = lhs;
    return _lhs;
}

template <class T> template <typename U>
Maths<T>::Maths(const Maths<U>& otherMaths){
    _lhs = otherMaths._lhs;
}

template <class T>
Maths<T>::~Maths(){}

template <class T> template <typename U>
Maths<T> Maths<T>::operator+(const Maths<T>& rhs){ return Maths._lhs + rhs; }

template <class T> template <typename U>
Maths<T> Maths<T>::operator-(const Maths<T>& rhs){ return Maths._lhs - rhs; }

template <class T> template <typename U>
Maths<T> Maths<T>::operator*(const Maths<T>& rhs){ return Maths._lhs * rhs; }

问题出在 VS 中,它无法识别关键字运算符(即不显示为蓝色),这是为什么?

编辑:

我已经删除了下面指出的错误。将所有定义移动到 .h 文件中,代码仍然无法编译,此处发现错误:http://i.imgur.com/Z9rWOFh.png

新代码(如果有兴趣的话):

//in Maths.h file
template <class T> class Maths{
public:
    Maths<T>(T lhs);

    template<typename U>
    Maths<T>(const Maths<U>& otherMaths);

    ~Maths();

    T& getValue(){ return _lhs; };

    template <typename U>
    Maths<T>& operator+(const Maths<U>& rhs);

    template <typename U> 
    Maths<T>& operator*(const Maths<U>& rhs);

    template <typename U> 
    Maths<T>& operator-(const Maths<U>& rhs);

private:
    T _lhs;
};

template <class T>
Maths<T>::Maths(T lhs){
    _lhs = lhs;
}

template <class T> template <typename U>
Maths<T>::Maths(const Maths<U>& otherMaths){
    _lhs = otherMaths.getValue();
}

template <class T>
Maths<T>::~Maths(){}

template <class T>
Maths<T> Maths<T>::operator+(const Maths<T>& rhs){ return _lhs + rhs.getValue(); }

template <class T> template <typename U>
Maths<T> Maths<T>::operator-(const Maths<U>& rhs){ return _lhs - rhs.getValue(); }

template <class T> template <typename U>
Maths<T> Maths<T>::operator*(const Maths<U>& rhs){ return _lhs * rhs.getValue(); }

//in main.cpp

#include "Maths.h"

int main(){
    Maths<int> x = 1;
    Maths<int> y = 5;
    x + y;
    return 0;
}

最佳答案

  1. 您不能在多个文件中分离模板类的声明和实现。因此,您需要将所有实现移至 header 。
  2. 你有很多现在没有看到的错误(例如在构造函数中返回)。我建议您在类声明中移动实现。更正它、调试、做一些测试,然后如果您仍然需要它,请尝试将实现移到外面。
  3. Visual Studio(或本例中的 IntelliSense)有时会在突出显示时出错(或者速度很慢)。不要专注于此。如果有问题,编译器会给你更准确的错误和警告。

我可以告诉你的一些错误:

  1. return在构造函数中 Maths<T>( T lhs ) ;
  2. return Maths._lhs + rhs; - Maths是一个类,但是您对实例进行操作。如果您需要指向当前实例的指针,请使用 this->_lhs , 或者只是 _lhs ;
  3. _lhs = otherMaths._lhs; - 您无法访问 private field ;可以得到一个值_lhs类(class) Maths< T > ,但是 Maths< U >是一个不同的类。所以你需要做一些像T& value( ) { return _lhs; }这样的功能并在这里使用它;

编辑:

还有一些错误。正如您在错误描述中看到的那样,您的实现

template <class T>
Maths<T> Maths<T>::operator+(const Maths<T>& rhs){ return _lhs + rhs.getValue(); }

不符合函数定义

// in class template <class T> class Maths
template <typename U>
Maths<T>& operator+(const Maths<U>& rhs);

(这就像一个游戏——找不同=D) 正确的做法是这样的:

// declaration
template <typename U>
Maths<T> operator+(const Maths<U>& rhs);
// ...
// definition
template <class T>
template <typename U>
Maths<T> Maths<T>::operator+(const Maths<U>& rhs) { return _lhs + rhs.getValue( ); }

我已经删除了 &从声明中添加 template <typename U>到防御。对于 operator-operator*你需要删除 & . 下一个问题你会得到 getValue没有常量声明。您需要添加新方法。

const T& getValue( ) const { return _lhs; }

关于c++ - 模板化类然后重载运算符 (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26971596/

相关文章:

java - 使用预先附加的附件启动用户的标准邮件客户端

C++。 1类具有变化的参数

c++ - 输入未清除

c++ - windows 命令提示符中的伪环境

swift - 创建主开关以更改所有背景颜色

c++ - 如何在 c++03 中获得模板参数的最精确表示?

c++ - 为什么这个简单的模板代码会出错?

html - 如何将长 HTML 拆分为 "functions"

c++ - 在 opencv 中录制视频仅 30 秒

Java { Object a = new Object(); 之间的区别a.getVariable() } 和 { new Object().getVariable() }