-2
#include <iostream>
#include <complex>
using namespace std;

int main(){
    complex<double> p;
    cin >> p.real() >> p.imag();
}

g++4.7.2 では正常に動作しますが、C++11 ではコンパイルに失敗しました。なんで?

次のエラーメッセージが表示されます。

prog.cpp: In function ‘int main()’:
prog.cpp:7:19: error: no match for ‘operator>>’ in ‘std::cin >> p.std::complex<double>::real()’

フルバージョン: http://ideone.com/M3BhVR

4

2 に答える 2

7

次のようにさらに簡単にできます。

cin >> p;

フォーマットは次のとおりです: (real,imag) (参照:ここ)

または、次の操作を実行できます。

double real, imag;
cin >> real >> imag;
complex<double> p(real, imag);
于 2013-07-17T16:57:51.873 に答える