SLIGHTLY URGENT Structure Mutator Issue
Aug 5, 2015 at 11:01am UTC
So basically I'm having this issue where my program is giving an error saying "real" was not declared within the scope in the setReal function. I've commented where I'm getting the problem.
Can someone show/tell me what I'm doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
//Complex.h file
#ifndef COMPLEX_H
#define COMPLEX_H
class Complex
{
public :
Complex();
Complex(double r, double i);
void getData();
void showData();
double getReal();
void setReal(double r);
private :
double real;
double imag;
};
#endif // COMPLEX_H
//Complex.cpp file
#include "Complex.h"
#include <iostream>
#include <iomanip>
using namespace std;
#include "Complex.h"
Complex::Complex(): real(0), imag(0)
{
//initialization of complex data to 0
}
Complex::Complex(double r, double i): real(r), imag(i)
{
//intialization of complex object
}
void Complex::getData()
{
cout << "Enter real value: " ;
cin >> real;
cout << "Enter imaginary value: " ;
cin >> imag;
return ;
}
void Complex::showData()
{
cout << "Complex Number: " << real << " + " << imag << "i" << endl;
return ;
}
void setReal(double r)
{
real = r; // THE PROBLEM
return ;
}
double Complex::getReal()
{
return real;
}
Last edited on Aug 5, 2015 at 11:08am UTC
Aug 5, 2015 at 11:13am UTC
Compare lines 58 and 65. One is a stand-alone function and the other is a member of class Complex ...
PS. The get-members should preferably be const
.
Aug 5, 2015 at 11:17am UTC
Thanks a ton! I'm apparently blind...
Topic archived. No new replies allowed.