Hello again.

I am wanting to make a program that handles complex numbers.

I have created a class that represents a complex number and called it, ComplexNum.

I have tried to create a second class that is the calculator and takes two ComplexNum objects as input and returns a third ComplexNum object that is the result.

I compiled the first class, ComplexNum without errors, but the second class is proving to be more difficult. I think I don't understand correctly how to pass an object to an object.

I have included the four files and the command line outputs from trying to compile them.

Thank you for your help!

ComplexNum.h

#ifndef COMPLEXNUM_H
#define COMPLEXNUM_H

class ComplexNum {

    public:
        ComplexNum(); // constructor
        void setValues(double, double); // member function for setting values
        void printValues(); // member function for printing the number
        ~ComplexNum(); // deconstructor

    private:
        double real; // holds real part of complex number
        double img; // holds imaginary part of complex number

};

#endif

ComplexNum.cpp

#include <iostream>

using std::cout;
using std::endl;

#include "ComplexNum.h"

ComplexNum::ComplexNum()
{
    // Initialize to be the complex zero, 0 + 0i
    real = img = 0;
}

// This sets the values to the user defined values
void ComplexNum::setValues(double realValue, double imgValue)
{
    // Set the complex number to be the user defined values
    real = realValue;
    img = imgValue;
}

// This prints the value of the complex number
void ComplexNum::printValues()
{
    // Print out value of the complex number, call it Z
    cout << "Z = " << real << " + " << img << "i" << endl;
}

ComplexCalc.h

#ifndef COMPLEXCALC_H
#define COMPLEXCALC_H

// forward declaration to include ComplexNum class
// Not sure if this is needed.
class ComplexNum;

class ComplexCalc{

    public:
        ComplexCalc(); // Constructor
        // adding complex numbers, uses ComplexNum as a type and needs a
        // reference, I think, same for subtraction
        ComplexNum addComplex( ComplexNum &, ComplexNum & );
        ComplexNum subComplex( ComplexNum &, ComplexNum & );
        ~ComplexCalc();

    private:
        // 3 complexnum objects to hold inputs and answer
        ComplexNum cNum1;
        ComplexNum cNum2;
        ComplexNum result;

};

#endif

ComplexCalc.cpp

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

#include "ComplexCalc.h"
#include "ComplexNum.h"

// Empty constructor
ComplexCalc::ComplexCalc(){}

// Function for adding
ComplexNum ComplexCalc::addComplex( ComplexNum &first, ComplexNum &second )
{
    cNum1 = first;
    cNum2 = second;
    result.real = cNum1.real + cNum2.real;
    result.img = cNum1.img - cNum2.img;

    return result;
}

// Function for substracting
ComplexNum ComplexCalc::subComplex( ComplexNum &first, ComplexNum &second )
{
    cNum1 = first;
    cNum2 = second;
    result.real = cNum1.real - cNum2.real;
    result.img = cNum1.img - cNum2.img;

    return result;
}

Here is the output for compiling the ComplexNum.cpp into an object.

owen@Aineko:~/programming/cplusplus/Complex$ g++ -c ComplexNum.cpp -o ComplexNum.o
owen@Aineko:~/programming/cplusplus/Complex$

Now, here is the output for trying to compile the second file, ComplexCalc.cpp into an object.

owen@Aineko:~/programming/cplusplus/Complex$ g++ -c ComplexCalc.cpp -o ComplexClac.o
In file included from ComplexCalc.cpp:7:
ComplexCalc.h:20: error: field ‘cNum1’ has incomplete type
ComplexCalc.h:21: error: field ‘cNum2’ has incomplete type
ComplexCalc.h:22: error: field ‘result’ has incomplete type
ComplexCalc.cpp: In member function ‘ComplexNum ComplexCalc::addComplex(ComplexNum&, ComplexNum&)’:
ComplexCalc.cpp:16: error: ‘cNum1’ was not declared in this scope
ComplexCalc.cpp:17: error: ‘cNum2’ was not declared in this scope
ComplexCalc.cpp:18: error: ‘result’ was not declared in this scope
ComplexCalc.cpp: In member function ‘ComplexNum ComplexCalc::subComplex(ComplexNum&, ComplexNum&)’:
ComplexCalc.cpp:27: error: ‘cNum1’ was not declared in this scope
ComplexCalc.cpp:28: error: ‘cNum2’ was not declared in this scope
ComplexCalc.cpp:29: error: ‘result’ was not declared in this scope
owen@Aineko:~/programming/cplusplus/Complex$

What's the point of using cNum1 and cNum2, you already have the variables first and second to use.
You can do the same thing for both functions

ComplexNum ComplexCalc::subComplex( ComplexNum &first, ComplexNum &second )
{
	ComplexNum* result = new ComplexNum();
	result->setValues(first.real - second.real, first.img - second.img);
	return *result;
}

But if you want to keep your code as-is just include the header for ComplexNum in the ComplexCalc.h because right now it doesn't know what ComplexNum is so it's an "incomplete type"

It should also be noted that you can't do this outside of the ComplexNum class

result.real = cNum1.real - cNum2.real;
result.img = cNum1.img - cNum2.img;

You declared real and img to be private members. You can't access them without some sort of accessor method ie., getReal, getImg. So define those functions or make the members public

This article has been dead for over six months. Start a new discussion instead.