我正在編寫無線通信仿真平臺的C++代碼。我們使用IT ++的定點算術庫(http://itpp.sourceforge.net/4.3.1/group__fixed.html)。我們的目標是使浮點和定點算術被包裝在統一的類(名爲num_c)中,並且可以爲num_c類編寫算法。模糊運算符時,專門std ::複雜與自定義類型
num_c類是從num_base_c派生的模板化類。 num_base_c類封裝了一個IT ++ Fix(http://itpp.sourceforge.net/4.3.1/classitpp_1_1Fix.html)對象,看起來像這樣(請原諒我不能直接複製粘貼我的代碼,因爲它們位於安全網絡中,我會盡量使代碼列表儘可能準確):
class num_base_c
{
public:
// Default constructor
num_base_c(bool fix=false, double x=0.0, int w=itpp::MAX_WORDLEN, int s=0, itpp::emode e=itpp::TC, itpp::o_mode o=itpp::SAT, itpp::q_mode q=itpp::RND, itpp::Stat *ptr=0);
// Other constructors ...
// Conversion operator
operator double() const;
// Overloaded operators (including +=, -=, *=, -, +)
protected:
bool m_flag_fixed; // true means fixed point number
double m_double;
itpp::Fix m_fix;
};
的num_c類看起來是這樣的:
// fix=true means fixed point value
template <bool fix, int w=16, int s=0, itpp::e_mode e=itpp::TC, itpp::o_mode o=itpp::SAT, itpp::q_mode q=itpp::RND>
class num_c : public num_base_c
{
public:
// Default constructor
explicit num_c(double x=0.0, itpp::Stat *ptr=0) : num_base_c(fix, x, w, s, e, o, q, ptr) {}
// Other constructors ...
// Constructor from int
num_c(const int &x);
// Different versions of overloaded operator = (assignment from num_base_c, itpp::fixrep, or int)
};
我想確認是否STD的操作::當num_c專業複雜的將正常工作,所以我測試了以下:
using namespace std;
using namespace itpp;
complex< num_c<true, 16, 8, TC, SAT, RND> > c1(num_c<true, 16, 8, TC, SAT, RND>(1.3125, 0), num_c<true, 16, 8, TC, SAT, RND>(2.0625, 0));
num_c<true, 16, 8, TC, SAT, RND> n1(0, 0);
n1 = abs(c1);
在編譯過程中,Visual Studio 2008提供了以下錯誤(只摘錄許多C2666錯誤中的一個): c:\ program files \ microsoft visual studio 9.0 \ vc \ include \ xcomplex(204):error C2666:「operator < 「:2個重載具有類似於轉化 ... \ num_c_operators.h(16):可能是 '布爾運算符<(常量num_base_c &,常量num_base_c &)' 或 '原生的C++運算<(雙,INT)'
在我的num_c_operators.h中,在第16行,我聲明如下:
extern bool operator<(const num_base_c& op1, const num_base_c& op2);
我認爲上述運算符<加上「運算符double()const」和「num_c(const int & x)」是編譯器試圖匹配運算符<(double,int)的原因。不過,我確實需要num_base_c的重載運算符<。我還需要轉換運算符來從int中加倍和構造函數(否則編譯器會報告std :: complex的其他錯誤)。我不想寫一個完全不同的複雜類(實際上IT ++提供了一個),因爲現有的算法代碼使用std :: complex。那麼我該怎麼做才能解決這個問題?
很多很多謝謝! 彭
可以使構造函數和轉換操作符明確? – jrok
感謝您的回答。恐怕這不容易。我們正在使用VS2008。據我所知顯式轉換運算符是自Visual Studio 2013以來支持的C++ 11功能。但如果我錯了,請糾正我。 – Peng