Link to home
Start Free TrialLog in
Avatar of urobins
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(double,double,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::subtractNumbers(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::printNumbers(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::printNumbers': function call missing argument list; use '&complexNumber::printNumbers' 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::printNumbers': function call missing argument list; use '&complexNumber::printNumbers' 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,double,double);
      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::complexNumber(void)
{
      realNumber1=0;
      realNumber2=0;
      imaginaryNumber1=0;
      imaginaryNumber2=0;
      realTotal=0;
      imaginaryTotal=0;

}

complexNumber::~complexNumber(void)
{
}

void complexNumber::setValues(double passedReal1,double passedReal2,double passedImaginary1,double passedImaginary2);
{
      realNumber1=passedReal1;
      realNumber2=passedReal2;
      imaginaryNumber1=passedImaginary1;
      imaginaryNumber2=passedImaginary2;
}

void complexNumber::addNumbers();
{
      realTotal= realNumber1 + realNumber2;
      imaginaryTotal= imaginaryNumber1 + imaginaryNumber2;

}
void complexNumber::subtractNumbers();
{
      realTotal= realNumber1 - realNumber2;
      imaginaryTotal= imaginaryNumber1 - imaginaryNumber2;
}

void complexNumber::printNumbers();
{
      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.setValues(userReal1,userReal2,userImaginary1,userImaginary2);
      userComplexNumber.addNumbers();
      cout << "Results of addition are" <<endl;
      userComplexNumber.printNumbers;
      userComplexNumber.subtractNumbers();
      cout << "Results of subtraction are" <<endl;
      userComplexNumber.printNumbers;

}
ASKER CERTIFIED SOLUTION
Avatar of UrosVidojevic
UrosVidojevic
Flag of Serbia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And one more thing,

userComplexNumber.printNumbers;

in main program should be

userComplexNumber.printNumbers();
Avatar of urobins
urobins

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.
Avatar of urobins

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?
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.

 
Avatar of urobins

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);//constructor
      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(const 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\Debug\Complex.exe : fatal error LNK1120: 1 unresolved externals
sorry post last comment to wrong topic Sorry!!!!