在C++ Builder XE2中使用TDictionary

Wil*_*lly 3 c++ dictionary c++builder c++builder-xe2

目前我想TDitionary在C++ Buillder XE2中使用

在我阅读文档后,我认为应该很容易,但我甚至无法创建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;
}
Run Code Online (Sandbox Code Playgroud)

错误消息:

[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
Run Code Online (Sandbox Code Playgroud)

任何人有任何想法?谢谢!

Rem*_*eau 7

就像@mhtaqia所说的那样,C++还不能实例化Delphi的Generics类,只有当它们由Delphi代码创建时才会使用它们.对于C++代码,您应该使用STL std::map代替:

#include <map> 

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

要么:

#include <map> 

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

旁注:不要在C++中使用TForm::OnCreateTForm::OnDestroy事件.它们是Delphi习惯用法,可以在C++中产生非法行为,因为它们可以分别在派生构造函数和派生析构函数之后触发.请改用实际的构造函数/析构函数.