c++ - 错误 LNK2020 : unresolved token (06000002) in Visual C++

标签 c++ visual-c++ compiler-errors c++-cli

我在 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/

相关文章:

c++ - VS 2010 中的复数问题

compiler-errors - 构造函数VDM++中的错误

c++ - 将Leap Motion Controller 与QT创建器一起使用

java - 将 OpenCV 转换为 JavaCV - 在函数 cvHoughLines2() 中需要帮助

c++ - 在 GCC 8 上工作的 Constexpr 计数器,并且不限于命名空间范围

c++ - 使用Opencv提取图像的公共(public)部分

c++ - 在 Linux 上使用 C++ 检测唤醒计算机

visual-c++ - 是否可以强制 CMake/MSVC 对没有 BOM 的源文件使用 UTF-8 编码? C4819

c++ - 如何将 TCHAR 数组与字符串连接起来?

android - alertDialog 无法正常工作