Reputation: 22995
Please have a look at the following code
Main.cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
system("pause");
return 0;
}
Magic.h
#pragma once
class Magic
{
public:
Magic();
~Magic();
virtual void display()=0;
};
Spell.h
#pragma once
#include "Magic.h"
#include <iostream>
#include <string>
using namespace std;
class Spell :
public Magic
{
public:
Spell(void);
Spell(string words);
~Spell(void);
void display();
private:
string words;
};
Spell.cpp
#include "Spell.h"
#include "Magic.h"
#include <iostream>
#include <string>
using namespace std;
Spell::Spell(void)
{
}
Spell::Spell(string words)
{
this->words = words;
}
Spell::~Spell(void)
{
cout << "Delete Spell" << endl;
}
void Spell::display()
{
cout << "Spell Words: " << words << endl;
}
Here, I am getting the error
1>------ Build started: Project: Revision1_1, Configuration: Debug Win32 ------
1>Spell.obj : error LNK2019: unresolved external symbol "public: __thiscall Magic::~Magic(void)" (??1Magic@@QAE@XZ) referenced in function __unwindfunclet$??0Spell@@QAE@XZ$0
1>Spell.obj : error LNK2019: unresolved external symbol "public: __thiscall Magic::Magic(void)" (??0Magic@@QAE@XZ) referenced in function "public: __thiscall Spell::Spell(void)" (??0Spell@@QAE@XZ)
1>C:\Users\yohan\Documents\Visual Studio 2010\Projects\Revision1_1\Debug\Revision1_1.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I do not understand what to do here. Why is this happening? Please help! I am new to C++ anyway..
Upvotes: 0
Views: 225
Reputation: 3830
You don't have an implementation for Magic. If your intention is for Magic to be an abstract base class, then just change its declaration to:
#pragma once
class Magic
{
public:
virtual void display()=0;
};
Remember, any method that is not followed by = 0 in the interface must be implemented in the class.
Upvotes: 1
Reputation: 59811
Magic
is not implementing its constructor and destructor (which also should be virtual
).
Don't even declare the constructor if not necessary, e.g.
class Magic {
public:
virtual ~Magic() {}
virtual void display() = 0;
};
Unrelated: I didn't know you can display magic.
Upvotes: 3
Reputation: 51345
You have declared a destructor in your Magic
class but did not define it. That's why the linker complains (and the compiler doesn't).
Upvotes: 2
Reputation: 258548
You didn't implement
Magic();
~Magic();
You'll need to either implement them inline, in an implementation file, or mark them = default
.
Upvotes: 1