Why am I getting 'undelcared identifier' ???

This is a discussion on Why am I getting 'undelcared identifier' ??? within the C++ Programming forums, part of the General Programming Boards category; Hello all, I am creating a complex number class. Here is the header file 'Complex.h' : Code: //February 15, 2006 ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    64

    Why am I getting 'undelcared identifier' ???

    Hello all,

    I am creating a complex number class. Here is the header file 'Complex.h' :

    Code:
    //February 15, 2006
    //Complex class declaration and definition file
    
    class Complex
    {
    	double real, imag;
    public:
    	Complex();
    	Complex(const Complex&);		//Copy constructor (so you can do '=' @ declaration)
    	Complex(const double&);				//Conversion constructor
    	Complex(const double&, const double&);
    	Complex conj(const Complex&);
    	Complex operator+(const Complex&);
    	Complex operator-(const Complex&);
    	Complex operator*(const Complex&);
    	Complex operator/(Complex&);
    	Complex operator=(const Complex&);
    	double Re(const Complex&);
    	double Im(const Complex&);
    	friend ostream& operator <<(ostream&, Complex&);
    	friend istream& operator >>(istream&, Complex&);
    };
    
    Complex::Complex()
    {
    	real = imag = 0.;
    }
    
    Complex::Complex(const Complex &a)
    {
    	real = a.real;
    	imag = a.imag;
    }
    
    Complex::Complex(const double &a)
    {
    	real = a;
    	imag = 0;
    }
    
    Complex::Complex(const double &a, const double &b)
    {
    	real = a;
    	imag = b;
    }
    
    Complex Complex::operator +(const Complex &num)
    {
    	real = real + num.real;
    	imag = imag + num.imag;
    	return *this;
    }
    
    Complex Complex::operator -(const Complex &num)
    {
    	real = real - num.real;
    	imag = imag - num.imag;
    	return *this;
    }
    
    Complex Complex::conj(const Complex &another)
    {
    	real = another.real;
    	imag = -another.imag;
    	return *this;
    }
    
    Complex Complex::operator *(const Complex &num)
    {
    	double a = real, b = imag, c = Re(num), d = Im(num);
    	real = a*c - b*d;
    	imag = a*d + b*c;
    	return *this;
    }
    
    Complex Complex::operator /(Complex &den)
    {
    	Complex den_star = conj(den);
    	double new_den = Re(den_star * den);
    	*this = *this * den_star;
    	real = real / new_den;
    	imag = imag / new_den;
    	return *this;
    }
    
    Complex Complex::operator =(const Complex &another)
    {
    	real = another.real;
    	imag = another.imag;
    	return *this;
    }
    
    double Complex::Re(const Complex &a)
    {
    	return a.real;
    }
    
    double Complex::Im(const Complex &a)
    {
    	return a.imag;
    }
    Here is the main cpp file:

    Code:
    #include <iostream.h>
    #include "Complex.h"
    
    ostream& operator <<(ostream &out, Complex &a)
    {
    	if(a.real != 0)
    		out<<a.real;
    	if(a.imag < 0)
    		out<<" - j"<<-a.imag;
    	if(a.imag > 0)
    		out<<" + j"<<a.imag;
    	if(a.real == 0 && a.imag == 0)
    		out<<"0";
    	return out;
    }
    
    istream& operator >>(istream &in, Complex &a)
    {
    	in>>a.real>>a.imag;
    	return in;
    }
    
    int main()
    {
    	Complex a(3,5);
    	cout<<a<<endl;
    	Complex b(-1,-2);
    	cout<<b<<endl;
    
    	cout<<"----------------------"<<endl;
    	cout<<conj(a)<<endl;
    
    	return 0;
    }
    For some reason, I am getting an "undeclared identifier" error in the main program when I call the conj(a) function, but it is clearly defined in the main program (and it is a very simple function). Maybe the answer is right in front of me, but I've been looking at it so much that I just don't see anything anymore. Thanks for all your help.

    Best,

    Bill83

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Code:
    cout<<conj(a)<<endl;
    conj is a member of the complex class you would call it like this
    Code:
    cout<<a.conj(a)<<endl;

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    64
    Thanks a lot. Like I said, the answer was right there. Most of my questions are this stupid, due to excessive programming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 10:23 PM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21