3

TDitionary現在、C ++BuillderXE2で使用したい

ドキュメントを読んだ後、簡単なはずだと思いましたが、TDictionaryオブジェクトを作成することさえできません...

私のコード:

#include <vcl.h>
#pragma hdrstop
#include <Generics.collections.hpp>
#include "TDictionaryTest.h"

#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;

void __fastcall TForm2::FormCreate(TObject *Sender)
{
    TDictionary__2 <String, String> *Dir = new  TDictionary__2<String, String>(0);
    delete Dir;
}

エラーメッセージ:

[ILINK32 Error] Error: Unresolved external '__fastcall           System::Generics::Collections::TDictionary__2<System::UnicodeString,   System::UnicodeString>::~TDictionary__2<System::UnicodeString, System::UnicodeString>()'  referenced from ...\PRACTICE\C++\WIN32\DEBUG\TDICTIONARYTEST.OBJ
[ILINK32 Error] Error: Unresolved external '__fastcall System::Generics::Collections::TEnumerable__1<System::Generics::Collections::TPair__2<System::UnicodeString, System::UnicodeString> >::~TEnumerable__1<System::Generics::Collections::TPair__2<System::UnicodeString, System::UnicodeString> >()' referenced from ...\PRACTICE\C++\WIN32\DEBUG\TDICTIONARYTEST.OBJ
[ILINK32 Error] Error: Unresolved external 'System::Generics::Collections::TDictionary__2<System::UnicodeString, System::UnicodeString>::' referenced from ...\PRACTICE\C++\WIN32\DEBUG\TDICTIONARYTEST.OBJ
[ILINK32 Error] Error: Unresolved external '__fastcall System::Generics::Collections::TDictionary__2<System::UnicodeString, System::UnicodeString>::TDictionary__2<System::UnicodeString, System::UnicodeString>(int)' referenced from ...\PRACTICE\C++\WIN32\DEBUG\TDICTIONARYTEST.OBJ
[ILINK32 Error] Error: Unable to perform link

誰かが何か考えを持っていますか?ありがとう!

4

2 に答える 2

7

@mhtaqiaが言ったように、C ++はDelphiのGenericsクラスをまだインスタンス化できず、Delphiコードによって作成された場合にのみそれらを消費します。C ++コードの場合、std::map代わりにSTLを使用する必要があります。

#include <map> 

void __fastcall TForm2::FormCreate(TObject *Sender) 
{ 
    std::map<String, String> *Dir = new std::map<String, String>; 
    delete Dir; 
} 

または:

#include <map> 

void __fastcall TForm2::FormCreate(TObject *Sender) 
{ 
    std::map<String, String> Dir; 
}

補足:C++でおよびイベントを使用しないでください。これらは、派生コンストラクタの前と派生デストラクタの後にそれぞれトリガーされる可能性があるため、C++で不正な動作を引き起こす可能性のあるDelphiイディオムです。代わりに、実際のコンストラクタ/デストラクタを使用してください。TForm::OnCreateTForm::OnDestroy

于 2012-05-16T18:23:39.917 に答える
2

TDictionaryDelphiの変数とフィールドにアクセスするためだけのものです。C++コードで使用およびインスタンス化することはできません。テンプレートクラスは、ヘッダーファイルで完全に定義されている場合に使用できます。

于 2012-05-16T06:36:51.073 に答える