其他分享
首页 > 其他分享> > 在C Builder XE2中使用TDictionary

在C Builder XE2中使用TDictionary

作者:互联网

目前我想在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

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

解决方法:

就像@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中产生非法行为,因为它们可以分别在派生构造函数和派生析构函数之后触发.请改用实际的构造函数/析构函数.

标签:c,dictionary,cbuilder,cbuilder-xe2
来源: https://codeday.me/bug/20190723/1512920.html