F
F
fapchat2021-09-26 22:55:06
C++ / C#
fapchat, 2021-09-26 22:55:06

Why is it not possible to create an object on the stack?

I am sure that it is because of the line MobilePhone telephone;that I get the error LNK1120.

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


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


But why should just trying to create an instance on the stack cause any errors at all?

Here are the files:
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;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Airog, 2021-09-26
@fapchat

"reference to unresolved external symbol "public: __thiscall MobilePhone::~MobilePhone(void)" The
linker writes that the destructor is not defined. The constructor is defined, but the destructor is not.

J
jcmvbkbc, 2021-09-26
@jcmvbkbc

a reference to an unresolved external symbol "public: __thiscall MobilePhone::~MobilePhone(void)"
why should just trying to create an instance on the stack cause any errors at all?

Did you read the text of the error? What do you think happens when the life cycle of an object created on the stack ends (i.e. when main() exits in your case)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question