3

目前我想TDitionary在 C++ Builder 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;
}

错误信息:

[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 的泛型类,只有在 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; 
} 

或者:

#include <map> 

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

附带说明:永远不要在 C++ 中使用TForm::OnCreateandTForm::OnDestroy事件。它们是 Delphi 习惯用法,可以在 C++ 中产生非法行为,因为它们可以分别在派生构造函数之前和派生析构函数之后触发。请改用实际的构造函数/析构函数。

于 2012-05-16T18:23:39.917 回答
2

TDictionary仅用于访问 Delphi 变量和字段。您不能在 C++ 代码中使用和实例化它。模板类在完全定义在头文件中时可用。

于 2012-05-16T06:36:51.073 回答