2011-01-14 44 views
1

我已经开始对C++ language.I工作是很新的后援计划采取复数从用户并打印出来out.But它给我很多类似错误,复数打印程序出现故障

prog.cpp: In function ‘int main()’: 
prog.cpp:26: error: ‘GetReal’ was not declared in this scope 
prog.cpp:26: error: ‘SetReal’ was not declared in this scope 
prog.cpp:27: error: ‘GetImag’ was not declared in this scope 
prog.cpp:27: error: ‘SetImag’ was not declared in this scope 
prog.cpp:28: error: ‘print’ was not declared in this scope 
prog.cpp: At global scope: 
prog.cpp:34: error: expected unqualified-id before ‘)’ token 
prog.cpp:40: error: expected unqualified-id before ‘float’ 
prog.cpp:40: error: expected `)' before ‘float’ 
prog.cpp: In function ‘void SetImag(float)’: 
prog.cpp:64: error: ‘real’ was not declared in this scope 
prog.cpp: In function ‘void SetReal(float)’: 
prog.cpp:68: error: ‘imag’ was not declared in this scope 
prog.cpp: In function ‘void print()’: 
prog.cpp:73: error: ‘Cout’ was not declared in this scope 
prog.cpp:73: error: ‘real’ was not declared in this scope 
prog.cpp:73: error: ‘imag’ was not declared in this scope 

下面的代码:

/* 
Date=13 January 2011 
Program: To take a complex number from the user and print it on the screen */ 

/*Defining a class*/ 
#include <iostream> 
using namespace std; 
class complex 
{ 
float real; 
float imag; 
public: 
complex(); 
complex(float a,float b); 
float GetReal(); 
float GetImag(); 
void SetReal(); 
void SetImag(); 
void print(); 
}; 

int main() 
{ 
complex comp; 
SetReal(GetReal()); 
SetImag(GetImag()); 
print(); 
} 

complex() 
{ 
real=0; 
imag=0; 
} 

complex(float a,float b) 
{ 
real=a; 
imag=b; 
} 

float GetReal() 
{ 
float realdata; 
cout<<"Enter Real part:"<<endl; 
cin>>realdata; 
return realdata; 
} 

float GetImag() 
{ 
float imagdata; 
cout<<"Enter Imaginary part"<<endl; 
cin>>imagdata; 
return imagdata; 
} 

void SetImag(float a) 
{ 
real=a; 
} 
void SetReal(float b) 
{ 
imag=b; 
} 

void print() 
{ 
printf("The Complex number is %f+%fi",real,imag); 
} 
+1

是否有一个很好的理由不使用'的std :: complex`? – 2011-01-14 10:39:04

+0

对不起,但我没有任何关于标准的想法。 – 2011-01-14 10:49:58

+0

http://www.cplusplus.com/reference/std/complex/ – 2011-01-14 10:52:01

回答

5

由于GetReal()等人被宣布为complex类的一部分,你应该叫他们所创建的对象:

complex comp; 
comp.SetReal(comp.GetReal()); 
comp.SetImag(comp.GetImag()); 
comp.print(); 

同样,你需要范围complex构造函数的实现:

complex::complex() 
{ 
    real=0; 
    imag=0; 
} 

这同样适用于在您的文章中未示出的其他成员函数。

0

在您的主函数中,您需要在类的实例上调用GetReal和SetReal:

Complex comp; 
comp.SetReal(); 
... 

此外,您的方法体没有绑定到类,它们在全局命名空间中浮动。您需要定义他们:

void Complex::SetReal() {} //etc 

希望这有助于