0

I have code that is supposed to show addition, subtraction, etc. of complex numbers with no input from the user necessary. I have three classes: test.cpp, complex.cpp, and complex.h to run the program, define the constructors and methods, and create a header class respectively. However, when I run my code I get a series of errors that I've been trying to figure out for a while now.

complex.h

//complex class definition
#ifndef COMPLEX_H
#define COMPLEX_H


//class complex
class Complex
{
public:
    Complex(); //default no arg constructor
    Complex(double a); //one arg constructor
    Complex(double a, double b); //two arg constructor
    Complex operator+(const Complex &) const; //addition method
    Complex operator-(const Complex &) const; //subtraction method
    Complex operator*(const Complex &) const; //multiplication method
    Complex operator/(const Complex &) const; //division method
    void print() const; //output
private:
    double a; //real number
    double b; //imaginary number
}; //end class Complex

#endif

complex.cpp

#include "stdafx.h"
#include <iostream>
#include "complex.h"
using namespace std;

//no arg constructor
Complex::Complex()
{
    a = 0;
    b = 0;
}

//one arg instructor
Complex::Complex(double real)
{
    a = real;
    b = 0;
}

//two arg constructor
Complex::Complex(double real, double imaginary)
{
    a = real;
    b = imaginary;
}

//addition
Complex Complex::operator+(const Complex &number2) const
{
    return a + number2.a, b + number2.b;
}

//subtraction
Complex Complex::operator-(const Complex &number2) const
{
    return a - number2.a, b - number2.b;
}

//multiplication
Complex Complex::operator*(const Complex &number2) const
{
    return a * number2.a, b * number2.b;
}

//division
Complex Complex::operator/(const Complex &number2) const
{
    return a / number2.a, b / number2.b;
}

//output display for complex number
void Complex::print() const
{
    cout << '(' << a << ", " << b << ')';
}

test.cpp

#include <iostream>
#include <complex>
#include "complex.h"
#include "stdafx.h"
using namespace std;

int main()
{
    Complex b(1.0, 0.0);
    Complex c(3.0, -1.0);

    /*cout << "a: ";
    a.print();

    system ("PAUSE");*/

};

In test right now as the code shows the lower parts are commented out and I have attempted to only call two of the three constructors to see if I can get any of this working.

The errors I receive:

    error C2065: 'Complex' : undeclared identifier
    error C2065: 'Complex' : undeclared identifier
    error C2146: syntax error : missing ';' before identifier 'b'
    error C2146: syntax error : missing ';' before identifier 'c'
    error C3861: 'b': identifier not found  
    error C3861: 'c': identifier not found

I am trying to run this in Visual Studio 2010. Can someone please help?

PlainPat
  • 65
  • 1
  • 2
  • 10
  • 6
    did you try removing `#include `. I don't know if this is the problem but maybe there is some kind of conflict between your class and the system one. – Kevin MOLCARD Nov 13 '12 at 08:57
  • 2
    This comma in the return ("return a * number2.a, b * number2.b;") is not familiar to me. – Richard Nov 13 '12 at 09:09
  • @Richard: That should really be `Complex` constructor calls, otherwise the operators don't return the correct types. For the comma operator see e.g. http://stackoverflow.com/questions/54142/how-does-the-comma-operator-work – Benjamin Bannier Nov 13 '12 at 09:12
  • Do you have multiple versions of complex.h somewhere? Perhaps `#include complex.h` is not finding the correct version. Copy the Complex class declaration into the same file to check this. – hhbilly Nov 13 '12 at 09:55
  • Not that this is relevant to your compilation issue, but your definitions for multiplication and division are wrong (or at least not the usual definition for complex numbers). Is this going to cause other problems for you? – mtrw Nov 13 '12 at 10:27
  • I tried the provided code (as posted above) in VS 2010 and VS 2012 and with gcc 4.7.2 and everything works fine. This should be no c++ problem, but rather a problem with your settings in VS 2010. Just don't use precompiled headers! – MWid Nov 13 '12 at 11:10
  • @MWid Yeah this is what was happening. I'm still using the precompiled header I just made sure to put it at the top of the files. – PlainPat Nov 13 '12 at 18:19

4 Answers4

3

I think the problem is in your Complex.cpp. If there are errors compiling the code of the class, the class is unknown and so the identifier Complex, too.

After a quick view, everthing seem to be correct but your operator functions have some errors:

Incorrect:

    //addition
    Complex Complex::operator+(const Complex &number2) const
    {
        return a + number2.a, b + number2.b;
    }

Correct:

    //addition
    Complex Complex::operator+(const Complex &number2) const
    {          
        return Complex(a + number2.a, b + number2.b);
    }

The problems are the return types of your operator methods. They have to be the same as declared. Also you cannot return two variables at the same time. If you want to do so, you have to use structs or classes (by calling the constructor of your class Complex with the new values).

If you want to manipulate the members a and b, you cannot define the methods as "const". If you say a function is const, it means all class members values won't be manipulated by calling this method.

Gombat
  • 1,994
  • 16
  • 21
  • 1
    Everything is valid c++ syntax. But by the return statement `return a + number2.a, b + number2.b;` you do not get what you exspect. You get the object `Complex(b + number2.b)`. There is no problem with the return types, since `double` can be converted to type `Complex`. – MWid Nov 13 '12 at 10:41
  • You're right. But as you said, the problem is that you do not get what you expect. It is just somehow bad "luck", that PlainPat provided a constructor with one double as argument. This way he didn't find the error by the compiler output as I first thought PlainPat could do so. – Gombat Nov 13 '12 at 10:58
1

You may facing a problem with stdafx.h issue

At least you should try to put #include "stdafx.h before every other include Or you could try to set your project property to not use stdafx.h

1. right click on project and select Properties
2. go to C/C++ -> Precompiled Headers 
3. select "Not Using Precompiled Headers"
billz
  • 44,644
  • 9
  • 83
  • 100
1
#include <complex>
#include "complex.h"

That's a bad smell. It's possible that both those #include files use the same name for the header guard.

Try changing yours like this.

#ifndef MY_COMPLEX_H
#define MY_COMPLEX_H

.. And maybe rename your module from "complex" to something else, to avoid clashes with the system header file of the same name.

Roddy
  • 66,617
  • 42
  • 165
  • 277
0

Try making it as simple as possible. Then add parts back until you find your error.

Does this work?

#include "complex.h"

int main()
{
    Complex c(3.0, -1.0);

    return 0;
};
Peter Wood
  • 23,859
  • 5
  • 60
  • 99