简体   繁体   English

“无法解析的外部符号”错误

[英]“unresolved external symbol” error

Please have a look at the following code 请看下面的代码

Main.cpp 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 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.. 我还是C ++的新手。

Magic is not implementing its constructor and destructor (which also should be virtual ). Magic没有实现其构造函数和析构函数(也应该是virtual )。

Don't even declare the constructor if not necessary, eg 如果没有必要,甚至不要声明构造函数,例如

class Magic {
public:
  virtual ~Magic() {}
  virtual void display() = 0;
};

Unrelated: I didn't know you can display magic. 无关:我不知道您可以展示魔术。

You didn't implement 你没有实现

Magic();
~Magic();

You'll need to either implement them inline, in an implementation file, or mark them = default . 您需要在实现文件中内联实现它们,或将它们标记为= default

You have declared a destructor in your Magic class but did not define it. 您已在Magic类中声明了一个析构函数,但未定义它。 That's why the linker complains (and the compiler doesn't). 这就是链接器抱怨的原因(编译器没有抱怨)。

You don't have an implementation for Magic. 您没有Magic的实现。 If your intention is for Magic to be an abstract base class, then just change its declaration to: 如果您打算让Magic成为抽象基类,则只需将其声明更改为:

#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. 请记住,任何在接口中不等于= 0的方法都必须在该类中实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM