利用模板執行個體化解決LNK2022錯誤.

來源:互聯網
上載者:User

諸如這樣的問題

http://social.msdn.microsoft.com/Forums/ar-SA/vcgeneral/thread/a4490504-956f-44d0-a39c-3a4bd36a4aac


I have the following lines
of code in my header file of a C++ CLR application:


#include<cliext/vector>
using namespace cliext;
...
vector <String^>^ availVector;

I then have the following lines of code in my CPP file in a method :

availVector = gcnew vector<String^>();
availVectsr->push_pack("test");

when I tried to compile the program I get the following linker errors:

1>CommandFile.obj : error LNK2022: metadata operation failed (80131188) : Inconsistent field declarations in duplicated types (types: cliext.impl.vector_impl<System::String ^,0>; fields: _Myarray): (0x04000029).

在我的項目裡也出現了.找不到切實可行的解決辦法,最後想到一招注意,完美的解決了這個問題:

把所有使用

cliext::vector<MyClass^>

的地方替換成

MyClassVector;

然後

ref struct MyClassVector : public cliext::vector<MyClass^>{};

這樣就能避免LNK2022錯誤了.

發生錯誤的時候的代碼是

typedef cliext::vector<MyClass^> MyClassVector;

這是我想到的辦法,另外還有一種解決辦法,是從網路上搜尋到的

template ref class cliext::vector<MyClass^>;
typedef cliext::vector<MyClass^> item_unit_vectory;

主要將

template ref class cliext::vector<MyClass^>;

放在所有的代碼最前面(一般我放先行編譯標頭檔裡)就好

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.