我在 C++/CLI 中创建一个新的抽象类时遇到了一个奇怪的错误。有很多与此类似的问题,但没有一个答案对我有帮助。
在这个新类(class)中,我收到以下错误:
error LNK2020: unresolved token (06000002) Foo::execute
这是 h 文件:
#pragma once
using namespace System::IO::Ports;
using namespace System;
public ref class Foo
{
protected:
SerialPort^ port;
public:
Foo(SerialPort^ sp);
virtual array<Byte>^ execute();
};
这是cpp文件:
#include "StdAfx.h"
#include "Foo.h"
Foo::Foo(SerialPort^ sp)
{
this->port = sp;
}
请注意,当我注释掉 virtual array<Byte>^ execute();
时行,一切都完美编译。另外,当我删除 virtual
修饰符并添加 execute()
的实现在 cpp 文件中,它也能正常工作。
最佳答案
你自己已经给出了答案:
Also, when I remove the virtual modifier and add an implementation of execute() in the cpp-file, it works as well.
您在 header 中声明了方法 execute
,但缺少它的实现。这正是链接器错误告诉您的内容。
在这种情况下,声明为 virtual
并不重要。
如果你想创建一个抽象类,你可以在网上的许多文章中找到更多的细节(例如Wikibooks: Abstract Classes)
关于c++ - 错误 LNK2020 : unresolved token (06000002) in Visual C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15654450/