复数打印程序出现故障 我开始研究C++语言。我对它很陌生。程序从用户那里取了一个复数,然后打印出来。但是它给了我很多错误,比如。 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

复数打印程序出现故障 我开始研究C++语言。我对它很陌生。程序从用户那里取了一个复数,然后打印出来。但是它给了我很多错误,比如。 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,c++,C++,代码如下: /* 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

代码如下:

/*
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);
}
/*
日期=2011年1月13日
程序:从用户处获取复数并在屏幕上打印*/
/*定义类*/
#包括
使用名称空间std;
阶级情结
{
浮动真实;
浮动图像;
公众:
复合物();
复合物(浮子a、浮子b);
float GetReal();
float GetImag();
void SetReal();
void SetImag();
作废打印();
};
int main()
{
复合comp;
SetReal(GetReal());
SetImag(GetImag());
打印();
}
复合物()
{
实数=0;
imag=0;
}
复合体(浮子a、浮子b)
{
real=a;
imag=b;
}
float GetReal()
{
浮动真实数据;
cout由于
GetReal()
等被声明为
complex
类的一部分,您应该在创建的对象上调用它们:

complex comp;
comp.SetReal(comp.GetReal());
comp.SetImag(comp.GetImag());
comp.print();
类似地,您需要确定
复杂
构造函数的实现范围:

complex::complex()
{
  real=0;
  imag=0;
}
这同样适用于您文章中未显示的其他成员函数。

由于
GetReal()
等被声明为
complex
类的一部分,您应该在创建的对象上调用它们:

complex comp;
comp.SetReal(comp.GetReal());
comp.SetImag(comp.GetImag());
comp.print();
类似地,您需要确定
复杂
构造函数的实现范围:

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

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

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

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

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

希望这对您的主函数有所帮助,您需要在类的实例上调用GetReal和SetReal: e、 g

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

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

希望这有帮助

有没有理由不使用
std::complex
?对不起,我对std没有任何想法。有没有理由不使用
std::complex
?对不起,我对std没有任何想法。如果我需要创建另一个对象怎么办?我如何对其他对象执行相同的操作?@fahad,你给它一个不同的名称租赁名称,例如,
complex-anotherComp;anotherComp.SetReal(comp.GetReal());
如果我需要创建另一个对象怎么办?我如何对另一个对象执行相同的操作?@fahad,您给它一个不同的名称,例如,
complex-anotherComp;anotherComp.SetReal(comp.GetReal());
c++区分大小写,你知道吗?;-)c++区分大小写,你知道吗?;-)