0

Hi i'm getting the following errors:

Error 9 error LNK1120: 2 unresolved externals

Error 8 error LNK2019: unresolved external symbol "public: virtual __thiscall physics::~physics(void)" (??1physics@@UAE@XZ) referenced in function "public: virtual void * __thiscall physics::`scalar deleting destructor'(unsigned int)" (??_Gphysics@@UAEPAXI@Z)

Error 7 error LNK2019: unresolved external symbol "public: virtual __thiscall student::~student(void)" (??1student@@UAE@XZ) referenced in function __unwindfunclet$??0physics@@QAE@XZ$0

which occur using the following code:

#include <iostream>
#include <string>
#include <vector>
using namespace std;



class student{
protected:
    string fName,sName;
    int id;
    vector<string> cname;
    vector<int> cmark;
public:
    virtual ~student();
    virtual void addCourse(string name, int mark)=0;
};


class physics : public student{
public:
    physics(){//Initialize default values
        fName="blank";
        sName="blank";
        id=0;
        cname.push_back("none");
        cmark.push_back(0);
    };
    ~physics();

    void addCourse(string name, int mark){
        if(cname.size()==1){//override default value for a size of 1
            cname[0]=name;
            cmark[0]=mark;
        }
        else{
            cname.push_back(name);
            cmark.push_back(mark);
        }
    }

};

The above compiles fine but when i try to initialize an object in main() by using:

int main(){

    //Database Storage
    vector<student*> DB;
    DB.push_back(new physics);
}

That's when i get the errors (removing the push_back line fixes it but i need this for my program). What am i doing wrong?

Eduardo
  • 6,900
  • 17
  • 77
  • 121
  • Turns out adding braces to the end of the destructors fixed it. What difference does that make? ~student(){}; instead of ~student(); – Eduardo May 05 '13 at 15:19
  • The semicolon after `~student(){}` isn't illegal, but it is unnecessary and doesn't make sense either. You don't put it after the other inline function definitions either. – Ulrich Eckhardt May 05 '13 at 15:23

1 Answers1

2

Turns out adding braces to the end of the destructors fixed it. What difference does that make? (from the comments)

The difference is that in one case you have a declaration which lacks a definition; in the second case you provide a (empty) definition inline.

Trying to invoke a function that is declared but not defined (as in the first case) result in an unresolved reference error raised by the linker - after all, what should it do when a function invocation is found for a function whose implementation is not available?

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451