LNK2022:元数据操作失败:重复类型中的字段声明不一致 [英] LNK2022: metadata operation failed : Inconsistent field declarations in duplicated types
问题描述
我在编译C ++ .NET项目时遇到问题.
I have problem with compiling my C++ .NET project.
我已阅读>"LNK2022:元数据操作失败"令我发疯-这不是我的情况,因为就我而言,我无法编译一个项目-链接时该项目失败.我尝试了该主题的所有(两个)解决方案,但对我没有帮助.
I have read "LNK2022: metadata operation failed" driving me insane -- and this is not my case, because in my case i cannot compile one single project -- it fails at link time. i tried all (two) solutions from that topic and that didn't help me.
仅当我将类更改为模板类时,此错误才开始上升.我有Vector2Di(用于int类型)类,现在需要与float类型完全相同,所以我将其重命名为Vector2D并将其更改为使用模板,现在我有了:
This errors started to rise up just when i have changed the class to be a template class. i have Vector2Di (for int type) class and now need completely the same for float type, so i renamed it to Vector2D and changed it to use template, now i have:
template <class T>
public ref class Vector2D : NativeValue<irr::core::vector2d<T>>
{
...
}
typedef Vector2D<int> Vector2Di;
typedef Vector2D<float> Vector2Df;
它开始出现链接器错误:
And it started to apear linker errors:
错误LNK2022:元数据操作失败(80131188):重复类型中的字段声明不一致(类型:NativeValue>;字段:m_NativeValue):(0x04000058).
error LNK2022: metadata operation failed (80131188) : Inconsistent field declarations in duplicated types (types: NativeValue >; fields: m_NativeValue): (0x04000058).
和
错误LNK2022:元数据操作失败(8013118B):在重复类型中实现的接口不一致(类型:NativeValue>;接口:System.IDisposable):(0x09000005).
error LNK2022: metadata operation failed (8013118B) : Inconsistent implemented interfaces in duplicated types (types: NativeValue >; interfaces: System.IDisposable): (0x09000005).
这两种类型的错误.
总之,Vector2D打算成为C ++值类型类vector2d的包装.NET类(也是模板).我必须将所有功能重定向到包装的类,所以我需要存储它的值,但因为我不能在ref类中使用非托管的valuetype变量(编译错误,因此不可行),我在该valuetype上使用了一个指针,但应分配该指针并释放后,我设计了ref类NativeValue -它也是模板,它存储valuetype作为引用,并注意及时删除它.
In short details: Vector2D intend to be a wrapping .NET class for C++ valuetype class vector2d (which is template too). I have to redirect all the functionality to wrappered class so i need a store its value, BUT as i cannot have unmanaged valuetype variable in ref class (compile errors apears), i use a pointer on that valuetype, BUT this pointer should be allocated and deallocated somewhere, AND I designed ref class NativeValue -- it is template too, it stores the valuetype as a reference and takes care about deleting it in time.
在这里:
template <class T>
ref class NativeValue
{
public:
~NativeValue()
{
this->!NativeValue();
}
!NativeValue()
{
if (m_NativeValue != nullptr)
{
delete m_NativeValue;
m_NativeValue = nullptr;
}
}
internal:
T* m_NativeValue;
protected:
NativeValue() {}
};
此外,另一个奇怪的事情出现了.当我将这些类型的使用从cpp文件移动到标头时,它会编译确定-很奇怪.
In addition, another strange thing now comes up. It compiles OK when i move my usage of these types from cpp files to headers -- that odd.
我已经预编译了头文件stdafx.cpp,并且我在stdafx.h中包括了所有基本类型(例如Vector2D);那么每个简单的文件都包含stdafx.h并使用这些类型.
i have precompiled header stdafx.cpp, and i include all basic types (like Vector2D) in stdafx.h; then every single file simple includes stdafx.h and use these types.
请,如果您发现任何可能的错误,请告诉我. 谢谢.
Please, if you see anything possibly wrong -- tell me. Thank You.
推荐答案
您应该将模板定义和声明都放在头文件中.请参阅此,其中说明了它的工作原理
You should put the template definition and declaration both inside header file. Please refer this , it explain how it works.
这篇关于LNK2022:元数据操作失败:重复类型中的字段声明不一致的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!