#include <iostream>
#include <complex>
using namespace std;
class MyComplex{
private:
int real, img;
public:
MyComplex();
MyComplex(int,int);
~MyComplex();
void set(int,int);
void display();
};
MyComplex::MyComplex(){
cout << "i'm being constructed (default).\n";
real=img=0;
}
MyComplex::MyComplex(int r, int i){
cout << "i'm being constructed (parameterized).\n";
real=r;
img=i;
}
MyComplex::~MyComplex(){
cout << "I'm being destroyed\n";
}
void MyComplex::set(int r, int i){
real=r;
img=i;
}
void MyComplex::display(){
cout << real << "+i" << img << endl;
}
int main(){
MyComplex c1;
MyComplex c2(10,-8);
c1.set(2,9);
c1.display();
c2.display();
cout << "Magnitude"<< double abs(const complex) << endl;
}
第一次在这些论坛中,如果我的代码写得非常糟糕,我很抱歉,我只是一个初学者。
我在书上找到了一项要求你计算复数的大小的作业。
我收到此错误:
testcomplex.cpp: In function ‘int main()’:
testcomplex.cpp:50:25: ***error: expected primary-expression before ‘double’
cout << "Magnitude"<< double abs(const complex) << endl;***
答案 0 :(得分:2)
您似乎已经编写了函数声明而不是调用它。假设你实际上有一个名为abs
的函数,你可以通过在它的括号中传递一个变量来调用它:
cout << "Magnitude " << abs(c1) << endl;