2013-03-27 76 views
4

我在C++/CLI中創建了一個新的抽象類,並且遇到了一個奇怪的錯誤。有很多類似這個問題的問題,但沒有一個答案可以幫助我。錯誤LNK2020:Visual C++中未解析的令牌(06000002)

在這個新類中,我得到以下錯誤:

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修飾符並在cpp文件中添加​​的實現時,它也可以工作。

回答

6

你已經給了自己的答案:

Also, when I remove the virtual modifier and add an implementation of execute() in the cpp-file, it works as well.

你宣佈在頭的方法execute,但它的實現是缺少。這正是鏈接器錯誤告訴你的。 在這種情況下,聲明爲virtual並不重要。

如果你想創建一個抽象類,你可以在許多文章在網上找到進一步的細節(如Wikibooks: Abstract Classes

+0

謝謝。當我有足夠的聲望時,我會接受這個答案,並加以讚揚。 :-) – 2013-03-27 08:37:56

3

您必須執行方法或從頭中刪除聲明。 (虛擬關鍵字在這種情況下無所謂)

請問一個問題,如果您有任何問題。

相關問題