urobins
asked on
Problem writing a class to add/subtract/print complex numbers in C++
I am working on a homework assignement in c++ where I need to create a class that accepts values passed to create complex values. I then add/subtract and print these values. I have created everything ( I think it should work) but it isn compiling. I appreciate any help you guys can give, I am running visual studio 2005
The errors I am getting are.
>------ Build started: Project: week 7, Configuration: Debug Win32 ------
1>Compiling...
1>complexNumber.cpp
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(22) : error C2761: 'void complexNumber::setValues(d ouble,doub le,double, double)' : member function redeclaration not allowed
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(23) : error C2447: '{' : missing function header (old-style formal list?)
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(30) : error C2761: 'void complexNumber::addNumbers( void)' : member function redeclaration not allowed
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(31) : error C2447: '{' : missing function header (old-style formal list?)
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(36) : error C2761: 'void complexNumber::subtractNum bers(void) ' : member function redeclaration not allowed
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(37) : error C2447: '{' : missing function header (old-style formal list?)
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(42) : error C2761: 'void complexNumber::printNumber s(void)' : member function redeclaration not allowed
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(43) : error C2447: '{' : missing function header (old-style formal list?)
1>week7_main.cpp
1>d:\my documents\visual studio 2005\projects\week 7\week 7\week7_main.cpp(27) : error C3867: 'complexNumber::printNumbe rs': function call missing argument list; use '&complexNumber::printNumb ers' to create a pointer to member
1>d:\my documents\visual studio 2005\projects\week 7\week 7\week7_main.cpp(30) : error C3867: 'complexNumber::printNumbe rs': function call missing argument list; use '&complexNumber::printNumb ers' to create a pointer to member
My code is as follows:
Header File:
#pragma once
class complexNumber
{
public:
complexNumber(void);
~complexNumber(void);
void setValues(double,double,do uble,doubl e);
void addNumbers();
void subtractNumbers();
void printNumbers();
private:
double realNumber1;
double realNumber2;
double imaginaryNumber1;
double imaginaryNumber2;
double realTotal;
double imaginaryTotal;
};
Class File
#include "complexNumber.h"
#include <iostream>
#include <fstream>
using namespace std;
complexNumber::complexNumb er(void)
{
realNumber1=0;
realNumber2=0;
imaginaryNumber1=0;
imaginaryNumber2=0;
realTotal=0;
imaginaryTotal=0;
}
complexNumber::~complexNum ber(void)
{
}
void complexNumber::setValues(d ouble passedReal1,double passedReal2,double passedImaginary1,double passedImaginary2);
{
realNumber1=passedReal1;
realNumber2=passedReal2;
imaginaryNumber1=passedIma ginary1;
imaginaryNumber2=passedIma ginary2;
}
void complexNumber::addNumbers( );
{
realTotal= realNumber1 + realNumber2;
imaginaryTotal= imaginaryNumber1 + imaginaryNumber2;
}
void complexNumber::subtractNum bers();
{
realTotal= realNumber1 - realNumber2;
imaginaryTotal= imaginaryNumber1 - imaginaryNumber2;
}
void complexNumber::printNumber s();
{
cout << "The Total is: ";
cout << "("<<realTotal <<","<<imaginaryTotal<<"i) "<<endl;
}
Main program
#include "complexNumber.h"
#include <iostream>
#include <fstream>
using namespace std;
int main (void)
{
double userReal1;
double userReal2;
double userImaginary1;
double userImaginary2;
complexNumber userComplexNumber;
cout <<"Please enter your first real number: ";
cin >> userReal1;
cout << endl << "Please enter your first imaginary number: ";
cin >> userImaginary1;
cout << endl << "Please enter your second real number: ";
cin >> userReal2;
cout << endl << "Please enter your second imaginary number: ";
cin >> userImaginary2;
userComplexNumber.setValue s(userReal 1,userReal 2,userImag inary1,use rImaginary 2);
userComplexNumber.addNumbe rs();
cout << "Results of addition are" <<endl;
userComplexNumber.printNum bers;
userComplexNumber.subtract Numbers();
cout << "Results of subtraction are" <<endl;
userComplexNumber.printNum bers;
}
The errors I am getting are.
>------ Build started: Project: week 7, Configuration: Debug Win32 ------
1>Compiling...
1>complexNumber.cpp
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(22) : error C2761: 'void complexNumber::setValues(d
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(23) : error C2447: '{' : missing function header (old-style formal list?)
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(30) : error C2761: 'void complexNumber::addNumbers(
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(31) : error C2447: '{' : missing function header (old-style formal list?)
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(36) : error C2761: 'void complexNumber::subtractNum
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(37) : error C2447: '{' : missing function header (old-style formal list?)
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(42) : error C2761: 'void complexNumber::printNumber
1>d:\my documents\visual studio 2005\projects\week 7\week 7\complexnumber.cpp(43) : error C2447: '{' : missing function header (old-style formal list?)
1>week7_main.cpp
1>d:\my documents\visual studio 2005\projects\week 7\week 7\week7_main.cpp(27) : error C3867: 'complexNumber::printNumbe
1>d:\my documents\visual studio 2005\projects\week 7\week 7\week7_main.cpp(30) : error C3867: 'complexNumber::printNumbe
My code is as follows:
Header File:
#pragma once
class complexNumber
{
public:
complexNumber(void);
~complexNumber(void);
void setValues(double,double,do
void addNumbers();
void subtractNumbers();
void printNumbers();
private:
double realNumber1;
double realNumber2;
double imaginaryNumber1;
double imaginaryNumber2;
double realTotal;
double imaginaryTotal;
};
Class File
#include "complexNumber.h"
#include <iostream>
#include <fstream>
using namespace std;
complexNumber::complexNumb
{
realNumber1=0;
realNumber2=0;
imaginaryNumber1=0;
imaginaryNumber2=0;
realTotal=0;
imaginaryTotal=0;
}
complexNumber::~complexNum
{
}
void complexNumber::setValues(d
{
realNumber1=passedReal1;
realNumber2=passedReal2;
imaginaryNumber1=passedIma
imaginaryNumber2=passedIma
}
void complexNumber::addNumbers(
{
realTotal= realNumber1 + realNumber2;
imaginaryTotal= imaginaryNumber1 + imaginaryNumber2;
}
void complexNumber::subtractNum
{
realTotal= realNumber1 - realNumber2;
imaginaryTotal= imaginaryNumber1 - imaginaryNumber2;
}
void complexNumber::printNumber
{
cout << "The Total is: ";
cout << "("<<realTotal <<","<<imaginaryTotal<<"i)
}
Main program
#include "complexNumber.h"
#include <iostream>
#include <fstream>
using namespace std;
int main (void)
{
double userReal1;
double userReal2;
double userImaginary1;
double userImaginary2;
complexNumber userComplexNumber;
cout <<"Please enter your first real number: ";
cin >> userReal1;
cout << endl << "Please enter your first imaginary number: ";
cin >> userImaginary1;
cout << endl << "Please enter your second real number: ";
cin >> userReal2;
cout << endl << "Please enter your second imaginary number: ";
cin >> userImaginary2;
userComplexNumber.setValue
userComplexNumber.addNumbe
cout << "Results of addition are" <<endl;
userComplexNumber.printNum
userComplexNumber.subtract
cout << "Results of subtraction are" <<endl;
userComplexNumber.printNum
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, that is what I get for copying/pasting out of my header :) was trying to avoid typos. I'll try that and let you know.
ASKER
Yep that fixed the problem thank you so much.
another question if you don't mind. Is there a way to check and make sure my inputted numbers are numbers and not alpha or symbol characters?
another question if you don't mind. Is there a way to check and make sure my inputted numbers are numbers and not alpha or symbol characters?
If you try to enter some incorrect input in this program, for example "a1" for some number, program will terminate.
One solution is to read entire line and then convert it into double with function strtod: http://www.cplusplus.com/reference/clibrary/cstdlib/strtod.html.
Note that if user enters some incorrect value, strtod function will return 0. If you do not want this, you will have to write your own function for checking string format.
One solution is to read entire line and then convert it into double with function strtod: http://www.cplusplus.com/reference/clibrary/cstdlib/strtod.html.
Note that if user enters some incorrect value, strtod function will return 0. If you do not want this, you will have to write your own function for checking string format.
ASKER
Okay thanks! I appreciate it.
ok, have made a few changes, and am getting the following errors. What I was attempting to do is get it to a point where I could create a main and test just the constructor and the display method to aleast see that I had it so far, but as I said now I have errors.
Code is:
Complex.h
//prevent multiple inclusions of header file
#ifndef COMPLEX_H
#define COMPLEX_H
//Complex class definition
class Complex
{
public:
Complex(double realNumber, double imaginaryNumber);//constru ctor
Complex addComplex(const Complex&);//add member function
Complex subtractComplex(const Complex&);//subtract member function
void displayComplex(); //print member function
private:
double real;//declare real variable
double imaginary;//declare imaginary variable
};//end class Complex
#endif
Complex.cpp
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include <cmath>
#include "Complex.h"//include definition of class Complex from Complex.h
Complex::Complex(double firstNumber = 0.0, double secondNumber = 0.0)//Complex constructor definition
{
real= firstNumber;
imaginary = secondNumber;
}//end member function Complex
Complex Complex::addComplex(const Complex&)//add member function definition
{
//code goes here
return 0;
}//end member function addComplex
Complex Complex::subtractComplex(c onst Complex&)//subtract member function definition
{
//code goes here
return 0;
}//end member function subtractComplex
void Complex::displayComplex()
{
cout << "The Complex number is:";
cout << "(" << real << ", " << imaginary << ")" << endl;
}//end function displayComplex
Errors are:
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>D:\Homework\Visual Studio 2005\Projects\Complex\Debu g\Complex. exe : fatal error LNK1120: 1 unresolved externals
Code is:
Complex.h
//prevent multiple inclusions of header file
#ifndef COMPLEX_H
#define COMPLEX_H
//Complex class definition
class Complex
{
public:
Complex(double realNumber, double imaginaryNumber);//constru
Complex addComplex(const Complex&);//add member function
Complex subtractComplex(const Complex&);//subtract member function
void displayComplex(); //print member function
private:
double real;//declare real variable
double imaginary;//declare imaginary variable
};//end class Complex
#endif
Complex.cpp
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include <cmath>
#include "Complex.h"//include definition of class Complex from Complex.h
Complex::Complex(double firstNumber = 0.0, double secondNumber = 0.0)//Complex constructor definition
{
real= firstNumber;
imaginary = secondNumber;
}//end member function Complex
Complex Complex::addComplex(const Complex&)//add member function definition
{
//code goes here
return 0;
}//end member function addComplex
Complex Complex::subtractComplex(c
{
//code goes here
return 0;
}//end member function subtractComplex
void Complex::displayComplex()
{
cout << "The Complex number is:";
cout << "(" << real << ", " << imaginary << ")" << endl;
}//end function displayComplex
Errors are:
1>MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
1>D:\Homework\Visual Studio 2005\Projects\Complex\Debu
sorry post last comment to wrong topic Sorry!!!!
userComplexNumber.printNum
in main program should be
userComplexNumber.printNum