开发者

Complex number printing program malfunctioning

I have started working on C++ language.I am very new to it.The program takes a complex number from the user and prints it out.But its giving me many开发者_如何学Python errors like,

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

Here's the code:

/*
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);
}


Since GetReal() et al are declared as part of the complex class, you should call them on the object you created:

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

Similarly, you need to scope the implementation of the complex constructor:

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

The same applies to the other member functions not shown in your post.


In your main function you need to call GetReal and SetReal on an instance of the class: e.g.

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

Also, your method bodies aren't bound to the class, they're floating in the global namespace. You need to define them:

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

Hope this helps

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜