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?