I have problem with compiling my C++ .NET project.
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.
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:
error LNK2022: metadata operation failed (80131188) : Inconsistent field declarations in duplicated types (types: NativeValue >; fields: m_NativeValue): (0x04000058).
and
error LNK2022: metadata operation failed (8013118B) : Inconsistent implemented interfaces in duplicated types (types: NativeValue >; interfaces: System.IDisposable): (0x09000005).
this two types of errors.
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 s开发者_JAVA百科hould 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.
It is here:
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() {}
};
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.
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.
精彩评论