[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem writing a class to add/subtract/print complex numbers in C++

Posted on 2007-10-21
8
Medium Priority
?
1,586 Views
Last Modified: 2013-12-14
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;

}
0
Comment
Question by:urobins
  • 3
  • 3
  • 2
8 Comments
 
LVL 7

Accepted Solution

by:
UrosVidojevic earned 2000 total points
ID: 20119544
First of all,

this syntax of defining function is incorrect:

void complexNumber::subtractNumbers();
{
...
}

it should be:

void complexNumber::subtractNumbers()
{
...
}

without ';'. You made this mistake several times.
Please, correct this and let me know if it works.
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20119554
And one more thing,

userComplexNumber.printNumbers;

in main program should be

userComplexNumber.printNumbers();
0
 

Author Comment

by:urobins
ID: 20119566
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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:urobins
ID: 20119573
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?
0
 
LVL 7

Expert Comment

by:UrosVidojevic
ID: 20119674
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.

 
0
 

Author Comment

by:urobins
ID: 20119709
Okay thanks!  I appreciate it.
0
 

Expert Comment

by:rebelcowgirl_73
ID: 21059691
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
0
 

Expert Comment

by:rebelcowgirl_73
ID: 21059708
sorry post last comment to wrong topic Sorry!!!!
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question