0

I'm trying to get a class working to represent complex numbers. I'm trying to derive everything from a base class, Number, and I make two inherited classes from it, Real and Complex. I'm trying to put virtual functions in the Number base class so that I can implement them in the derived classes.

The problem is, when I go to build an instance of a class, I get the following error messages:

Error 2 error LNK1120: 1 unresolved externals Error 1 error LNK2019: unresolved external symbol "public: __thiscall Real::~Real(void)" (??1Real@@QAE@XZ) referenced in function _wmain

I've checked and I've seen that errors 1120 & 2019 can appear when your project linker subsystem is set to Windows instead of Console, but I've already made it as a Console Application. The errors above appear below when I try to create an instance of class Real. Thanks for any help.

    #include "stdafx.h"
    #include <iostream>
    #include <math.h>

    #define PI 3.14159

class Number
{
public:
    virtual void Add(double value)      { std::cout << "Add Numbers" << std::endl; }
    virtual void Sub(double value)      { std::cout << "Subtract Numbers" << std::endl; }
    virtual void Mult(double value)     { std::cout << "Multiply Numbers" << std::endl; }
    virtual void Div(double value)      { std::cout << "Divide Numbers" << std::endl; }
    virtual void SetVal(double value)   { std::cout << "Set Number Value" << std::endl; }
    virtual void PrintVal()             { std::cout << "Print Number Value" << std::endl; }
};

class Real : public Number
{
private:
    double real;

public:

    Real(double value)
    {
        Real::real = value;
    }

    ~Real();

     void Add(double value)
    {
        real += value;
    }

     void Sub(double value)
    {
        real -= value;
    }

     void Mult(double value)
    {
        real *= value;
    }

     void Div(double value)
    {
        real /= value;
    }

     void SetVal(double value)
    {
        real = value;
    }

     void PrintVal()
    {
        std::cout << "Number = " << real << std::endl;
    }
};

class Complex : public Number
{
private:
    double real;
    double complex;

    double FindAngle(double real, double complex)
    {
        if (real == 0 && complex > 0)
            return 0;

        else if (real == 0 && complex < 0)
            return PI;

        else if (real > 0 && complex == 0)
            return PI / 2;

        else if (real < 0 && complex == 0)
            return 3 * PI / 2;

        else
            return atan2(complex, real);
    }

public:

    Complex(double val1, double val2)
    {
        real = val1;
        complex = val2;
    }

    ~Complex();

     void Add(double val1, double val2)
    {
        real += val1;
        complex += val2;
    }
     void Sub(double val1, double val2)
    {
        real -= val1;
        complex -= val2;
    }

     void Mult(double val1, double val2)
    {
        double abs1, abs2, angle1, angle2;
        double absFinal, angleFinal;

        abs1 = sqrt(real*real + complex*complex);
        abs2 = sqrt(val1*val1 + val2 + val2);
        angle1 = FindAngle(complex, real);
        angle2 = FindAngle(val2, val1);

        absFinal = abs1 * abs2;
        angleFinal = angle1 + angle2;

        real = absFinal * cos(angleFinal);
        complex = absFinal * sin(angleFinal);

    }

     void Div(double val1, double val2)
    {
        double abs1, abs2, angle1, angle2;
        double absFinal, angleFinal;

        abs1 = sqrt(real*real + complex*complex);
        abs2 = sqrt(val1*val1 + val2 + val2);
        angle1 = FindAngle(complex, real);
        angle2 = FindAngle(val2, val1);

        absFinal = abs1 / abs2;
        angleFinal = angle1 - angle2;

        real = absFinal * cos(angleFinal);
        complex = absFinal * sin(angleFinal);
    }

     void SetVal(double val1, double val2)
    {
        real = val1;
        complex = val2;
    }

     void PrintVal()
    {
        std::cout << "Number = " << real << " + j" << complex << std::endl;
    }

};

int _tmain(int argc, _TCHAR* argv[])
{
    Real r = Real(3);
    system("PAUSE");
    return 0;
}
Copernicus
  • 291
  • 1
  • 2
  • 13
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – πάντα ῥεῖ Mar 13 '14 at 20:28

1 Answers1

2

Your Real::~Real() is undefined, it's declared, but it isn't defined.

DigitalEye
  • 1,456
  • 3
  • 17
  • 26