下面的代码不工作,我找不到原因,任何帮助将不胜感激。
//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;
}
最佳答案
- 您不能在多个文件中分离模板类的声明和实现。因此,您需要将所有实现移至 header 。
- 你有很多现在没有看到的错误(例如在构造函数中返回)。我建议您在类声明中移动实现。更正它、调试、做一些测试,然后如果您仍然需要它,请尝试将实现移到外面。
- Visual Studio(或本例中的 IntelliSense)有时会在突出显示时出错(或者速度很慢)。不要专注于此。如果有问题,编译器会给你更准确的错误和警告。
我可以告诉你的一些错误:
-
return
在构造函数中Maths<T>( T lhs )
; -
return Maths._lhs + rhs;
-Maths
是一个类,但是您对实例进行操作。如果您需要指向当前实例的指针,请使用this->_lhs
, 或者只是_lhs
; -
_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/