-3

I got some errors with the code below that made it impossible to run,It seems the error is in destructor but I don't know how to fix it, please check for me, I thank

#include<iostream>
#include<string.h>
using namespace std;


class SinhVien{
    private:
        static int msv;
        char name[256];
        char address[256];
        int phone;
        float point1, point2, point3;
    public:

        SinhVien(){
        }
        ~SinhVien();
        void input(){
            cout <<"Nhap ten: ";
            fflush(stdin);
            gets(name);
            cout <<"Nhap dia chi: ";
            fflush(stdin);
            gets(address);
            cout <<"Nhap so dien thoai: ";
            cin >>phone;
            cout <<"Nhap diem 3 mon: ";
            cin >>point1 >> point2 >> point3;
        }
        void output(){
            cout <<"Ma sinh vien: "<<msv<<endl;
            msv++;
            cout <<"Ho ten: "<<name<<endl;
            cout <<"Dia chi: "<<address<<endl;
            cout <<"So dien thoai: "<<phone<<endl;
            cout <<"Diem 3 mon: "<<point1<<" "<<point2<<" "<<point3<<endl;
        }

};

main(){
    SinhVien sv;
    sv.input();
    sv.output();
    system("pause");
} 

This is an error

C:\Users\Admin\AppData\Local\Temp\ccaMkngb.o    ex4.cpp:(.text+0x4e): undefined reference to `SinhVien::~SinhVien()'
C:\Users\Admin\AppData\Local\Temp\ccaMkngb.o    ex4.cpp:(.text+0x64): undefined reference to `SinhVien::~SinhVien()'
C:\Users\Admin\AppData\Local\Temp\ccaMkngb.o    ex4.cpp:(.rdata$.refptr._ZN8SinhVien3msvE[.refptr._ZN8SinhVien3msvE]+0x0): undefined reference to `SinhVien::msv'
C:\Users\Admin\Desktop\OOP\exclass\collect2.exe [Error] ld returned 1 exit status
Thomas Matthews
  • 54,980
  • 14
  • 94
  • 148
Khanam
  • 19
  • 3
  • Please copy & paste the error message _as text_ into the question. – churill Oct 29 '19 at 13:02
  • *"It seems the error is in destructor"* You didn't implement any destructor. If you don't want any destructor, then just remove the declaration. – Blaze Oct 29 '19 at 13:05
  • Unless your class is a base class and you want to provide an empty `virtual` destructor, you shouldn't code an empty destructor. If you know there is nothing to destroy, you're making things worse for the compiler's optimizer by telling it there is a user-defined destructor that does nothing. [See this](https://stackoverflow.com/questions/52283359/declaring-an-empty-destructor-prevents-the-compiler-from-calling-memmove-for-c) – PaulMcKenzie Oct 29 '19 at 13:14
  • Please [edit] your code to reduce it to a [mcve] of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. – Toby Speight Oct 29 '19 at 16:44

3 Answers3

4

You declare your destructor (~SinhVien();), but you never implement it.

If you don't need to do anything in the destructor, just don't declare it.

eike
  • 1,186
  • 6
  • 17
  • Thanks, but there's one more error I can't fix C:\Users\Admin\AppData\Local\Temp\ccaMkngb.o ex4.cpp:(.rdata$.refptr._ZN8SinhVien3msvE[.refptr._ZN8SinhVien3msvE]+0x0): undefined reference to `SinhVien::msv', Please check for me – Khanam Oct 29 '19 at 13:09
  • @Khanam [Undefined reference to static class member](https://stackoverflow.com/questions/272900/undefined-reference-to-static-class-member). – Algirdas Preidžius Oct 29 '19 at 13:28
4

For starters you have to define the static data member declared in the class

static int msv;

For example define it after the class definition like

int SinhVien::msv;

And the destructor is declared

~SinhVien();

but is not defined.

You could write instead in the class definition

~SinhVien() = default;

Also such a call

fflush(stdin);

has undefined behavior. Remove it.

Vlad from Moscow
  • 265,791
  • 20
  • 170
  • 303
  • Thanks, but there's one more error I can't fix C:\Users\Admin\AppData\Local\Temp\ccaMkngb.o ex4.cpp:(.rdata$.refptr._ZN8SinhVien3msvE[.refptr._ZN8SinhVien3msvE]+0x0): undefined reference to `SinhVien::msv', Please check for me – Khanam Oct 29 '19 at 13:09
  • 2
    @Khanam I already pointed out this in my answer. Reread it anew. – Vlad from Moscow Oct 29 '19 at 13:10
2

Errormessage is pretty clear: The destructor ~SinhVien(); is declared, but not defined. If you don't have anything to clean up, you don't need to declare a destructor. Or just change it to ~SinhVien() {}

churill
  • 10,318
  • 3
  • 14
  • 25
  • Thanks, but there's one more error I can't fix C:\Users\Admin\AppData\Local\Temp\ccaMkngb.o ex4.cpp:(.rdata$.refptr._ZN8SinhVien3msvE[.refptr._ZN8SinhVien3msvE]+0x0): undefined reference to `SinhVien::msv', Please check for me – Khanam Oct 29 '19 at 13:09
  • You spammed this comment now under every answer, even the one that actually answered this question, _before_ you asked. Why not simply google the error code? – churill Oct 29 '19 at 13:14