[英]Templating a class and then overloading operators (C++)
下面的代碼無法正常工作,我無法找到原因,我們將不勝感激。
//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;
}
我可以告訴您一些錯誤:
return
構造函數Maths<T>( T lhs )
; return Maths._lhs + rhs;
- Maths
是一類,但是您可以使用實例進行操作。 如果您需要獲取指向當前實例的指針,請使用_lhs
this->_lhs
,或僅使用_lhs
; _lhs = otherMaths._lhs;
-您無法訪問private
字段; 您可以得到Maths< T >
類的值_lhs
,但是Maths< U >
是不同的類。 因此,您將需要執行一些函數,例如T& value( ) { return _lhs; }
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; }
kes ...您仍然沒有納入早期反饋。 下面是一些可編譯的代碼:
template <class T>
class Maths
{
public:
Maths(T lhs);
template<typename U>
Maths(const Maths<U>& otherMaths);
~Maths();
T& getValue() { return _lhs; };
const T getValue() const { 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> 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(); }
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;
}
請注意,編譯所需的更正包括向const
getValue
添加const
函數,更改運算符聲明以按值而不是按引用返回Math<T>
。
Math<int>
,在Math<double>
添加Math<int>
Math<double>
返回Math<int>
並沒有多大意義,因為這兩種類型的精度較低。 下面的代碼使用decltype
來選擇C ++本身對這兩種類型進行操作時使用的相同類型,因此,例如,將int
乘以double
返回double
,而添加到unsigned int
的uint8_t
(又稱為unsigned char
)將返回另一個unsigned int
。 我也_lhs
了n_
的_lhs
成員變量名稱,因為_lhs
僅從操作員內部的近視角度適用。...使用了rhs
的“ _lhs
”,無論如何也沒有任何意義...
template <class T>
class Maths
{
public:
Maths(T n);
template<typename U>
Maths(const Maths<U>& otherMaths);
~Maths();
T& getValue() { return n_; };
const T getValue() const { return n_; };
template <typename U>
Maths<decltype(T()+U())> operator+(const Maths<U>& rhs);
template <typename U>
Maths<decltype(T()*U())> operator*(const Maths<U>& rhs);
template <typename U>
Maths<decltype(T()-U())> operator-(const Maths<U>& rhs);
private:
T n_;
};
template <class T>
Maths<T>::Maths(T n){
n_ = n;
}
template <class T> template <typename U>
Maths<T>::Maths(const Maths<U>& otherMaths){
n_ = otherMaths.n_;
}
template <class T>
Maths<T>::~Maths(){}
template <class T> template <typename U>
Maths<decltype(T()+U())> Maths<T>::operator+(const Maths<U>& rhs)
{ return getValue() + rhs.getValue(); }
template <class T> template <typename U>
Maths<decltype(T()-U())> Maths<T>::operator-(const Maths<U>& rhs)
{ return getValue() - rhs.getValue(); }
template <class T> template <typename U>
Maths<decltype(T()*U())> Maths<T>::operator*(const Maths<U>& rhs)
{ return getValue() * rhs.getValue(); }
//in main.cpp
#include "Maths.h"
int main()
{
Maths<int> x = 1;
Maths<int> y = 5;
x + y;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.