目前我想在C ++ Buillder XE2中使用TDitionary
在我阅读documentation后,我认为应该很容易,但我甚至无法创建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
任何人都有任何想法?谢谢!
答案 0 :(得分: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;
}
或者:
#include <map>
void __fastcall TForm2::FormCreate(TObject *Sender)
{
std::map<String, String> Dir;
}
旁注:不要曾在C ++中使用TForm::OnCreate
和TForm::OnDestroy
事件。它们是Delphi习惯用法,可以在C ++中产生非法行为,因为它们可以分别在派生构造函数和派生析构函数之后触发。改为使用实际的构造函数/析构函数。
答案 1 :(得分:2)
TDictionary
仅用于访问Delphi变量和字段。您不能在C ++代码中使用和实例化它。模板类在头文件中完全定义时可用。