.net 托管C+中的未解析令牌+;
我手上有个谜。我正在努力学习来自C++背景的管理C++,并遇到了一个难题。如果我有一个包含两个类的项目,一个基类Soup和一个派生类TomatoSoup,我编译为一个静态库(.lib),我会在Soup中的虚拟方法上得到未解析的标记。代码如下:.net 托管C+中的未解析令牌+;,.net,.net-2.0,polymorphism,managed-c++,.net,.net 2.0,Polymorphism,Managed C++,我手上有个谜。我正在努力学习来自C++背景的管理C++,并遇到了一个难题。如果我有一个包含两个类的项目,一个基类Soup和一个派生类TomatoSoup,我编译为一个静态库(.lib),我会在Soup中的虚拟方法上得到未解析的标记。代码如下: 项目摘要 汤.h namespace Abstracts { public ref class Soup abstract { public: virtual void heat(int Degrees);
项目摘要 汤.h
namespace Abstracts
{
public ref class Soup abstract
{
public:
virtual void heat(int Degrees);
};
}
#include "Soup.h"
namespace Abstracts
{
public ref class TomatoSoup : Abstracts::Soup
{
public:
virtual void heat(int Degrees) override;
};
}
TomatoSoup.h
namespace Abstracts
{
public ref class Soup abstract
{
public:
virtual void heat(int Degrees);
};
}
#include "Soup.h"
namespace Abstracts
{
public ref class TomatoSoup : Abstracts::Soup
{
public:
virtual void heat(int Degrees) override;
};
}
TomatoSoup.cpp
#include "TomatoSoup.h"
void Abstracts::TomatoSoup::heat(int Degrees)
{
System::Console::WriteLine("Soup's on.");
}
#include "TomatoSoup.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Abstracts::TomatoSoup^ ts = gcnew Abstracts::TomatoSoup();
return 0;
}
Main.proj
Main.cpp
#include "TomatoSoup.h"
void Abstracts::TomatoSoup::heat(int Degrees)
{
System::Console::WriteLine("Soup's on.");
}
#include "TomatoSoup.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Abstracts::TomatoSoup^ ts = gcnew Abstracts::TomatoSoup();
return 0;
}
virtual void heat(int Degrees)=0;
virtual void heat(int Degrees){}
并获得一份未经参考的正式文件
参数警告被视为
错误Abstracts.Soup::heat
函数的定义。当您将函数声明为虚拟空穴热(int度)时代码>链接器将期望找到在某处定义的函数体
如果您不打算提供函数体,并要求子类最终重写函数,则应按照解决方案1中的说明执行:
virtual void heat(int Degrees) = 0;
这会告诉编译器您不打算实现该函数。当你这样做的时候会发生什么
另外,关于第二个解决方案:
virtual void heat(int Degrees) {}
编译器告诉您不在函数中引用Degrees参数。在C++中避免这种警告的一种方法是不给参数一个名称:
virtual void heat(int /*Degrees*/) {}
但是,提供空实现与纯虚拟(=0
)声明有点不同。我没有多少CLI经验,但我认为您应该使用public继承:
public ref类TomatoSoup:publicAbstracts::Soup我认为发生此错误可能是因为我试图将Abstracts.proj构建为静态库。当我把它改成.DLL时,一切都很好
我看到有人提到静态库不支持托管代码,但没有官方代码。有人有一个很好的参考书来说明这一点吗?谢谢!当我使用您的技术注释掉参数名称时,它起作用了,但是现在当我尝试从标题中拉出实现时,我得到了相同的链接错误。我会投票支持你并改变问题。也许足够好==>哦,老兄,我一直在到处寻找官方对这一点的认可,我希望我能投票支持评论。