簡體   English   中英

模版一個類,然后重載運算符(C ++)

[英]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;
}
  1. 您不能在多個文件中分開模板類的聲明和實現。 因此,您需要將所有實現都移至標頭。
  2. 您有許多現在看不到的錯誤(例如,返回構造函數)。 我建議您在類聲明中移動實現。 對其進行更正,調試,進行一些測試,然后在仍然需要時嘗試將實現移至外部。
  3. Visual Studio(在這種情況下為IntelliSense)有時會在突出顯示時出現一些錯誤(或者可能很慢)。 不要專注於此。 如果出現問題,編譯器會為您提供更精確的錯誤和警告。

我可以告訴您一些錯誤:

  1. return構造函數Maths<T>( T lhs ) ;
  2. return Maths._lhs + rhs; - Maths是一類,但是您可以使用實例進行操作。 如果您需要獲取指向當前實例的指針,請使用_lhs this->_lhs ,或僅使用_lhs
  3. _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 intuint8_t (又稱為unsigned char )將返回另一個unsigned int 我也_lhsn__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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM