Mar 4, 2013 at 5:04pm UTC
Getting this error, don't know how to fix it. Below are the 3 files I included in the project.
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
// Complx.cpp
#include "complx.h"
complx::~complx() {
complx complx::operator + (const complx& c) const
}
ostream &operator << ( ostream &out_file, complx &number ){
out_file<<number.real<<" i" <<number.imag<<endl;
return out_file
}
extern ifstream &operator >> ( ifstream &in_file, complx &number ){
double re, is;
char ch;
if (in_file >> ch && ch == '(' && in_file >> re >> ch && ch == ','
&& in_file >> is >> ch && ch == ')' )
number.Set(re,is);
else cerr << "Finish the input" <<endl;
return in_file;
}
extern istream &operator >> ( istream &in_file, complx &number ){
in_file>> &number.real >> &number.imag;
return in_file;
}
// define constructor
complx::complx( double r, double i ){
real = r; imag = i;
}
int operator == (const complx& a, const complx& b){
if ( a.real == b.real && a.imag==b.imag)
return 1;
else
return 0;
}
__________________________________________________________________
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
#include <iostream>
#include <fstream>
using namespace std;
class complx
{
double real,
imag;
public :
complx( double real = 0., double imag = 0.); // constructor
~complx(); // destrucutor
friend int operator == (const complx&, const complx&);
//Sets private data members.
void Set(double new_real, double new_imaginary) {
real = new_real;
imag = new_imaginary;
}
//Returns the real part of the complex number.
double Real() {
return real;
}
//Returns the imaginary part of the complex number.
double imaginary() {
return imag;
}
};
extern ifstream &operator >> ( ifstream &in_file, complx &number );
extern istream &operator >> ( istream &in_file, complx &number );
extern ostream &operator << ( ostream &out_file, complx &number );
_____________________________________________________________________
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//call_complx.cpp
#include "complx.h"
int main()
{
int i=0;
complx c1, c2(3,5),in[5];
cout<< "Please input a complex number in the format of (a,b) - " << endl;
while ((cin >> in[i])&& (i<4)){
cout << in[i] << endl;
i++;
}
if (c1 == in[0] )
cout << c1 << " equals to " << in[0] << endl;
else
cout << c1 << " not equal to " << in[0]<< endl;
if (in[1] == in[2] )
cout << in[1] << " equals to " << in[2] << endl;
else
cout << in[1] << " not equal to " << in[2]<< endl;
char c; cin >> c;
return 0; //successful termination
}
Last edited on Mar 4, 2013 at 5:18pm UTC
Mar 4, 2013 at 5:08pm UTC
Are you sure you are compiling and linking both cpp files?
Mar 4, 2013 at 5:19pm UTC
OK the linker error is fixed but i'm getting an error now like "8 C:\Dev-Cpp\Exercise 4\complx.h `double complx::real' is private "
Mar 4, 2013 at 5:21pm UTC
You need to make your operator>> and operator<< functions friend functions in your complx class.
Mar 4, 2013 at 5:24pm UTC
Line 7 of complx.cpp
out_file<<number.real<<" i" <<number.imag<<endl;
You are trying to access private data. Either use accessors: out_file << number.Real() << " i" << number.imaginary() << endl;
or declare operator<< as friend.
Last edited on Mar 4, 2013 at 5:25pm UTC