LNK2022: metadata operation failed : Inconsistent field declarations in duplicated types

此生再无相见时 提交于 2019-12-07 04:15:34

问题


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 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.

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.


回答1:


You should put the template definition and declaration both inside header file. Please refer this , it explain how it works.



来源:https://stackoverflow.com/questions/2865021/lnk2022-metadata-operation-failed-inconsistent-field-declarations-in-duplicat

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!