fapchat
@fapchat

Почему невозможно создать объект в стеке?

Я уверен в том, что именно из-за строки MobilePhone telephone; у меня вылазит ошибка LNK1120.
Ошибка	LNK1120	неразрешенных внешних элементов: 1	ConsoleApplication2	C:\Users\User\source\repos\2 курс\лабы по проге\лаба 3\ConsoleApplication2\Debug\ConsoleApplication2.exe	1


И LNK2019.
Ошибка	LNK2019	ссылка на неразрешенный внешний символ "public: __thiscall MobilePhone::~MobilePhone(void)" (??1MobilePhone@@QAE@XZ) в функции _main.	ConsoleApplication2	C:\Users\User\source\repos\2 курс\лабы по проге\лаба 3\ConsoleApplication2\ConsoleApplication2.obj	1


Но почему просто из-за попытки создать экземпляр в стеке вообще должны вылазить какие-либо ошибки?

Вот файлы:
main.cpp
#pragma once

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <locale.h>  
#include <iostream>
#include <cstdio>
#include <string>
#include <fstream>
#include "MobilePhone.h";


using namespace std;

int main()
{


setlocale(LC_ALL, "rus");


char model1[] = "Xirrr";
char Number1[] = "+38555555";
char LastNumber1[] = "";
double Cash1 = 322;
char CompanyManufacturer1[] = "Xiaomy";
char Weight1[] = "1kg";
char Color1[] = "black";
char Price1[] = "200$";

MobilePhone telephone; //не работает

MobilePhone* telephone1 = new MobilePhone(model1, Number1, LastNumber1, Cash1, CompanyManufacturer1, Weight1, Color1, Price1); // работает
}

MobilePhone.h
#pragma once
#include "Telephone.h"

class MobilePhone: public Telephone
{
public:
    MobilePhone();
    MobilePhone(char* model, char* number, char* lastNumber, double cash, char* companyManufacturer, char* weight, char* color, char* price);
    ~MobilePhone();

protected:
    char* Model;
    char* Number;
    char* LastNumber;
    double Cash;
    int Charging;
    char* CompanyManufacturer;
    char* Weight;
    char* Color;
    char* Price;

};

MobilePhone.cpp
#define _CRT_SECURE_NO_WARNINGS
#include"MobilePhone.h"
#include <iostream>



MobilePhone::MobilePhone()
{
    Charging = 100;
    count++;
}


MobilePhone::MobilePhone(char* model, char* number, char* lastNumber, double cash, char* companyManufacturer, char* weight, char* color, char* price)
{
    Model = model;
    Number = number;
    LastNumber = lastNumber;
    Cash = cash;
    Charging = 100;
    CompanyManufacturer = companyManufacturer;
    Weight = weight;
    Color = color;
    Price = price;
    count++;
}

Telephone.h
#pragma once
#include "ICallable.h"

using namespace std;
class Telephone: public ICallable
{

public:

    void serialize();
    void serialize(const string& filename);
    void deserialize();
    void deserialize(const string& filename);
    virtual void call(const std::string& recepient);



    void setModel(char* model);
    void setNumber(char* number);
    void setLastNumber(char* lastNumber);
    void setCash(double cash);
    void setCompanyManufacturer(char* companyManufacturer);
    void setWeight(char* weight);
    void setColor(char* color);
    void setPrice(char* price);

    char* getModel();
    char* getNumber();
    char* getLastNumber();
    double getCash();
    char* getCompanyManufacturer();
    char* getWeight();
    char* getColor();
    char* getPrice();
    int getCharging();



    static int count;

protected:
    char* Model;
    char* Number;
    char* LastNumber;
    double Cash;
    int Charging;
    char* CompanyManufacturer;
    char* Weight;
    char* Color;
    char* Price;
};

Telephone.cpp
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include "Telephone.h"
#include <stdio.h>
#include <locale.h>  
#include <iostream>
#include <cstdio>
#include <string>
#include <fstream>

using namespace std;


 void Telephone::serialize() 
 {
        ofstream fout;
        if (count == 1) {
            fout.open("output.txt", fstream::out);
            fout << Model << endl << Number << endl << LastNumber << endl << Cash;
            fout.close();
        }
        else {
            fout.open("output.txt", ios_base::app);
            fout << endl;
            fout << Model << endl << Number << endl << LastNumber << endl << Cash;
            fout.close();
        }

 }

    void Telephone::serialize(const string& filename) 
    {
        ofstream fout;
        fout.open(filename + ".txt", fstream::out);
        fout << Model << endl << Number << endl << LastNumber << endl << Cash;
        fout.close();

    }
    void Telephone::deserialize() 
    {
        fstream fin; //ifstream - file input
        fin.open("output.txt", fstream::in);
        int cnt = 0;
        std::string model;
        std::string number;
        std::string lastNumber;
        double cash;
        if (count == 1) {
            fin >> model;
            fin >> number;
            fin >> lastNumber;
            fin >> cash;
        }
        else {
            while (cnt < count) {
                fin >> model;
                fin >> number;
                fin >> lastNumber;
                fin >> cash;
                cnt++;
            }
        }

        char* cstr = new char[model.length() + 1];
        strcpy(cstr, model.c_str());

        Model = cstr;

        cstr = new char[number.length() + 1];
        strcpy(cstr, number.c_str());
        Number = cstr;

        cstr = new char[lastNumber.length() + 1];
        strcpy(cstr, lastNumber.c_str());
        LastNumber = cstr;

        Cash = cash;
    }

    void Telephone::deserialize(const string& filename)
    {
        fstream fin;
        fin.open(filename + ".txt", fstream::in);
        std::string model;
        std::string number;
        std::string lastNumber;
        double cash;

        fin >> model;
        fin >> number;
        fin >> lastNumber;
        fin >> cash;

        char* cstr = new char[model.length() + 1];
        strcpy(cstr, model.c_str());

        Model = cstr;

        cstr = new char[number.length() + 1];
        strcpy(cstr, number.c_str());
        Number = cstr;

        cstr = new char[lastNumber.length() + 1];
        strcpy(cstr, lastNumber.c_str());
        LastNumber = cstr;

        Cash = cash;

    }

    void Telephone::call(const std::string& recepient)
    {
        Charging -= 3;
        Cash -= 1;
        strcpy(LastNumber, recepient.c_str());
        int i = 0;

        while (recepient[i])
        {
            if (!recepient[i] >= '0' && !recepient[i] <= '9') { cout << "Номер некорректен!!!\n"; break; }
            i++;
        }

        cout << "произведён звонок на телефон с номером: " << recepient << endl;
    }

    void Telephone::setModel(char* model)
    {
        Model = model;
    }
    void Telephone::setNumber(char* number)
    {
        Number = number;
    }
    void Telephone::setLastNumber(char* lastNumber)
    {
        LastNumber = lastNumber;
    }
    void Telephone::setCash(double cash)
    {
        Cash = cash;
    }
    void Telephone::setCompanyManufacturer(char* companyManufacturer)
    {
        CompanyManufacturer = companyManufacturer;
    }
    void Telephone::setWeight(char* weight)
    {
        Weight = weight;
    }
    void Telephone::setColor(char* color)
    {
        Color = color;
    }
    void Telephone::setPrice(char* price)
    {
        Price = price;
    }


    char* Telephone::getModel()
    {
        return Model;
    }
    char* Telephone::getNumber()
    {
        return Number;
    }
    char* Telephone::getLastNumber()
    {
        return LastNumber;
    }
    double Telephone::getCash()
    {
        return Cash;
    }

    char* Telephone::getCompanyManufacturer()
    {
        return CompanyManufacturer;

    }
    char* Telephone::getWeight() 
    {
        return Weight;

    }
    char* Telephone::getColor()
    {
        return Color;

    }
    char* Telephone::getPrice()
    {
        return Price;

    }
    int Telephone::getCharging()
    {
        return Charging;
    }

int Telephone::count = 0;
  • Вопрос задан
  • 161 просмотр
Решения вопроса 1
@Airog
"ссылка на неразрешенный внешний символ "public: __thiscall MobilePhone::~MobilePhone(void)"

Линковщик пишет, что не определен деструктор. Конструктор определен, а деструктор нет.
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
jcmvbkbc
@jcmvbkbc
"I'm here to consult you" © Dogbert
ссылка на неразрешенный внешний символ "public: __thiscall MobilePhone::~MobilePhone(void)"
почему просто из-за попытки создать экземпляр в стеке вообще должны вылазить какие-либо ошибки?

Ты текст ошибки прочитал? Как ты думаешь, что происходит, когда заканчивается жизненный цикл объекта созданного на стеке (т.е. при выходе из main() в твоём случае)?
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы