C ++继承问题,错误LNK1120& LNK2019

时间:2014-03-13 20:22:48

标签: c++ inheritance

我试图让一个班级代表复杂的数字。我试图从基类Number中派生所有东西,然后我从中创建两个继承的类,Real和Complex。我试图将虚函数放在Number基类中,以便我可以在派生类中实现它们。

问题是,当我去构建一个类的实例时,我收到以下错误消息:

错误2错误LNK1120:1个未解析的外部 错误1错误LNK2019:未解析的外部符号" public:__ thiscall Real :: ~Real(void)" (?? 1Real @@ QAE @ XZ)在函数_wmain

中引用

我已经检查过,并且我已经看到错误1120&当您的项目链接器子系统设置为Windows而不是Console时,可能会出现2019,但我已将其设置为控制台应用程序。当我尝试创建Real类的实例时,上面的错误显示在下面。谢谢你的帮助。

    #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;
}

1 个答案:

答案 0 :(得分:2)

您的Real ::〜Real()未定义,已声明,但尚未定义。