0

Уже в который раз встречаю ошибки LNK2019, LNK2001, LNK1120.

main.cpp


    #include <iostream>
    #include <cmath>
    using namespace std;
#include &quot;Kilometer.h&quot;
#include &quot;Meter.h&quot;
#include &quot;Decimeter.h&quot;
#include &quot;Centimeter.h&quot;



#define MAX 10
int  main() {
    Kilometer* arr[MAX]; //масив покажчиків на базовий клас
    for (int i = 0; i &lt; MAX; i++) { arr[i] = NULL; } //обнулення масиву
    for (int i = 0; i &lt; MAX; i++)
    {
        if (i % 3 == 0 || i % 3 == 1 || i % 3 == 2) {
            arr[i] = new Centimeter(1 + rand() % MAX);
        } else if (i % 3 == 3 || i % 3 == 4 || i % 3 == 5 || i%3 == 10)
            arr[i] = new Decimeter(1 + rand() % MAX);
        else if (i % 3 == 6 || i % 3 == 7 || i % 3 == 8 || i% 3 == 9 )
            arr[i] = new Meter(1 + rand() % MAX);
        arr[i] -&gt; Square();
        arr[i] -&gt; Out();          //виклик методів для елементів масиву
    }
    for( int i = 0; i &lt; MAX; i++ )
        if( arr[i] ) delete arr[i]; //очищення масиву
    return 0;
}

Kilometer.h

    
        #pragma once
        class Kilometer{
        protected:
            double length;
        public:
            Kilometer (double length=0);
            virtual ~Kilometer () = 0;
            virtual void Out() = 0;
            virtual double Square() = 0;
    }

;

Meter.h


    #pragma once
    #include "Kilometer.h"
    class Meter: public Kilometer{
    public:
        Meter(double length);
        ~Meter() ;
        double Square();
        void Out();
    }

;

Decimeter.h


    #pragma once
    #include "Kilometer.h"
class Decimeter: public Kilometer{
public:
    Decimeter(double length);
    ~Decimeter();
    double Square();
    void Out();
};

Centimeter.h


    #pragma once
    #include "Kilometer.h"
class Centimeter: public Kilometer{
public:
    Centimeter(double length);
    ~Centimeter();
    double Square();
    void Out();
};

Kilometer.cpp

    #include "Kilometer.h"
    #include <iostream>
using namespace std;

Kilometer::Kilometer(double length) {
    this-&gt;length = length;
}

Meter.cpp

    
        #include <iostream>
        #include "Meter.h"
        using namespace std;
    Meter::Meter(double length) : Kilometer(length)
    {
        this-&gt;length = length;
    }
    Meter::~Meter(){}
    double Meter::Square()
    {
        return length * 1000 * 1000;
    }
    void Meter::Out()
    {
       cout&lt;&lt;&quot;the square area in meters is :\t&quot;&lt;&lt;Square()&lt;&lt;endl;
    }

Decimeter.cpp

```

#include #include "Decimeter.h" using namespace std;

Decimeter::Decimeter(double length) : Kilometer(length)
{
    this->length = length;
}
Decimeter::~Decimeter(){}
double Decimeter::Square()
{
    return length * 10000 * 10000;
}
void Decimeter::Out()
{
    cout<<"the square area in decimeters is :\t"<<Square()<<endl;
}

Centimeter.cpp

        #include <iostream>
        #include "Centimeter.h"
        using namespace std;

        Centimeter::Centimeter(double length) : Kilometer(length)
        {
            this->length = length;
        }
        Centimeter::~Centimeter(){};
        double Centimeter::Square()
        {
            return length * 100000 * 100000;
        }
        void Centimeter::Out()
        {
            cout<<"the square area in centimeters is :\t"<<Square()<<endl;
        }

и выходит такое

Kromster
  • 13,809

1 Answers1

1

Понимаете, с деструктором ваш фокус

virtual ~Kilometer () = 0;

не проканает: деструктор предка должен вызываться обязательно. Вот и все ваши проблемы...

virtual ~Kilometer () {};

вполне сойдет.

Harry
  • 221,325