gpt4 book ai didi

c++ - "unresolved external symbol"错误

转载 作者:太空宇宙 更新时间:2023-11-04 15:06:27 25 4
gpt4 key购买 nike

请看下面的代码

main.cpp

#include <iostream>
#include <string>

using namespace std;

int main()
{
system("pause");

return 0;
}

魔法.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;
}

在这里,我得到了错误

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 ==========

我不明白这里要做什么。为什么会这样?请帮忙!无论如何,我是 C++ 的新手..

最佳答案

Magic 没有实现其构造函数和析构函数(它们也应该是 virtual)。

如无必要,甚至不要声明构造函数,例如

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

无关:我不知道你会施展魔法。

关于c++ - "unresolved external symbol"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14004123/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com