[英]C++11 copy assignment for std::complex in g++ 4.5 - no match for 'operator+'
[英]C++ “no match for operator >>” when comiling on -std=c++11
我具有將文件中的數據(數字)加載到復雜表的功能。 一切都在-std = c ++ 98上進行編譯而沒有錯誤,但是當我想使用-std = c ++ 11進行編譯時,會出現帶有運算符>>的probem。
template <typename T> void load(char name[], complex<T> table[], int len) {
ifstream dane;
dane.open(name);
for (int i = 0; i < 2 * len; i++)
(i % 2 == 0 ? dane >> table[i / 2].real() : dane >> table[(i - 1) / 2].imag());
dane.close();
}
no match for 'operator>>' in 'dane >> (table + ((sizetype)(((unsigned int)((i + -1) / 2)) * 16u)))->std::complex<double>::imag()'
no match for 'operator>>' in 'dane >> (table + ((sizetype)(((unsigned int)(i / 2)) * 16u)))->std::complex<double>::real()
在此之下,存在着許多候選人的信息,這些候選人無法將論據從雙精度轉換為雙精度。
那么,如何使用c ++ 11標准運行它呢?
http://en.cppreference.com/w/cpp/numeric/complex/imag
這些都不是返回引用,因此該值不是左值而是rvalue(我相信),並且您不能將其賦給rvalues(想象一下,寫dane dane >> 5;
;,同樣的方法。您將必須讀入臨時變量,然后根據我你會寫到real
或imag
。
(編寫示例: table[i /2].real(myTemporaryVariable);
)
編輯:
工作功能:
template <typename T> void load(char name[], complex<T> table[], int len) {
ifstream dane;
dane.open(name);
for (int i = 0; i < 2 * len; i++)
{
double read;
dane >> read;
if (!(i%2)) table[i / 2].real(read);
else table[(i - 1) / 2].imag(read);
}
dane.close();
}
我也不知道為什么用-std = c ++ 99編譯
使用C ++ 11,std :: complex的real()和imag()成員成為constexpr,這意味着const。 因此,不再為他們提供運算符>>。 有關規范,請參見http://en.cppreference.com/w/cpp/numeric/complex/imag 。 我看不到這種構造如何在C ++ 11中工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.