2011-01-14 4 views
1

Я начал работать на C++ language.I очень новый для it.The программы занимает комплексное число от пользователя и выводит его 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

Есть хорошая причина не использовать 'зОго :: complex'? –

+0

Извините, но я не имею ни малейшего представления о std. –

+0

http://www.cplusplus.com/reference/std/complex/ –

ответ

5

с GetReal() и др объявлены в рамках complex класса, вы должны вызвать их на объект, созданный:

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

Кроме того, вы должны объем реализации complex конструктору:

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

То же самое относится и к другим функциям-членам, не показанных в сообщении.

+0

Что делать, если мне нужно создать другой объект? Как бы я сделал то же самое для другого объекта, чем? –

+0

@fahad, вы даете ему другое имя, например. 'complex anotherComp; anotherComp.SetReal (comp.GetReal()); ' –

+0

О, теперь я получаю его. Спасибо –

0

В вашей основной функции вам нужно вызвать GetReal и SetReal в экземпляре класса: например.

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

Кроме того, тела метода не связаны с классом, они плавают в глобальном пространстве имен. Вы должны определить их:

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

Надеется, что это помогает

+0

C++ чувствителен к регистру, вы знаете? ;-) –

Смежные вопросы