Click to See Complete Forum and Search --> : Why does my function not a member?


nicoletonyf
October 11th, 2004, 10:27 AM
Hello there, why do I have this message when I try to use subTwoComplex function in my testing file? thanks
error C2039: 'subTwoComplex' : is not a member of 'Complex'
*********header file
#ifndef COMPLEX_H
#define COMPLEX_H

class Complex {

public:
Complex( double = 4.5, double = 5.5 );
Complex addTwoComplex( Complex );
Complex subTwoComplex( Complex );
void print();
void setTwoParts( double, double);
void setReal(double);
void setImaginary(double);

private:
double realPart;
double imaginaryPart;
};

#endif
#include <iostream>
using namespace std;
//using std::cout;

#include "complex.h"

Complex::Complex(double real, double imaginary)
{
realPart = real;
imaginaryPart = imaginary;
}
void Complex::setReal(double r)
{
realPart = r;
}
void Complex::setImaginary(double i)
{
imaginaryPart = i;
}
void Complex::setTwoParts(double really, double imag)
{
setReal(really);
setImaginary(imag);
}

Complex Complex::addTwoComplex( Complex a )
{
Complex addition;
addition.realPart = a.realPart + realPart;
addition.imaginaryPart = a.imaginaryPart + imaginaryPart;
return addition;
}
Complex Complex::subTwoComplex( Complex c )
{
Complex subtraction;
subtraction.realPart = c.realPart - realPart;
subtraction.imaginaryPart = c.imaginaryPart - imaginaryPart;
return subtraction;
}
void Complex::print()
{
cout << "(" << realPart << ", " << imaginaryPart << ")";
}
************testing file
#include <iostream>
using namespace std;


#include "complex.h"

int main()
{

Complex cmplx1;
cout << "Default constructor is : \n" ;
cmplx1.print();

Complex cmplx2 ( 4.4, 5.5 );
Complex cmplx3 ( 2.2, 3.3 );

cout << '\n';
Complex cmplx4 = cmplx2.addTwoComplex(cmplx3);
cmplx4.print();

cout << '\n';
Complex cmplx5 = cmplx3.subTwoComplex(cmplx2);
cmplx5.print();

return 0;
}

avinash_sahay
October 11th, 2004, 10:46 AM
I made three files Complex.h, Complex.cpp and TestFile.cpp
In Complex.h, I put the definition of class Complex.
In Complex.cpp, I put the source code for the functions of Complex.
In TestFile.cpp, I put main.

Then I compiled. It did not give any error.

NoHero
October 11th, 2004, 12:18 PM
also worked for me ... I think you should use the code tags and constant reference when passing 'Complex' objects to a member function ...:


Complex Complex::addTwoComplex( const Complex& a )
{
Complex addition;
addition.realPart = a.realPart + realPart;
addition.imaginaryPart = a.imaginaryPart + imaginaryPart;
return addition;
}


just a tip :cool:

wien
October 11th, 2004, 05:36 PM
Is there any reason why you are making your own complex number class, when the C++ standard already has one? (#include <complex>)