【发布时间】:2020-04-21 21:40:57
【问题描述】:
希望你能帮我解决这个编译错误。
我正在尝试测试取自 Embarcadero 官方文档网站的代码,该网站旨在测试 TIniFile 类。
但是我得到了这个错误:
Unit2.cpp(76): parsing: TCustomIniFile * _fastcall Form2::OpenIniFileInstance().
下面是我的代码:
#include <vcl.h>
#pragma hdrstop
#include "Unit2.h"
#include <IniFiles.hpp>
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
__fastcall TForm2::TForm2(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TForm2::btStoreClickClick(TObject *Sender)
{
//First Edit of the file
/* Open an instance */
TCustomIniFile* SettingsFile = OpenIniFileInstance();
// Store current form properties to be used in later sessions.
try
{
SettingsFile->WriteInteger (Name, "Top", Top);
SettingsFile->WriteInteger (Name, "Left", Left);
SettingsFile->WriteInteger (Name, "Width", Width);
SettingsFile->WriteInteger (Name, "Height", Height);
SettingsFile->WriteString (Name, "Caption", Caption);
SettingsFile->WriteBool (Name, "InitMax", WindowState == wsMaximized );
}
catch(Exception* e)
{
}
delete SettingsFile;
}
void __fastcall TForm2::btLoadClick(TObject *Sender)
{
TCustomIniFile* SettingsFile = OpenIniFileInstance();
try
{
/*
Read all saved values from the last session. The section name
is the name of the form. Also use form's properties as defaults
*/
Top = SettingsFile->ReadInteger(Name, "Top", Top );
Left = SettingsFile->ReadInteger(Name, "Left", Left );
Width = SettingsFile->ReadInteger(Name, "Width", Width );
Height = SettingsFile->ReadInteger(Name, "Height", Height );
Caption = SettingsFile->ReadString (Name, "Caption", Caption);
// Load last window state
if (SettingsFile->ReadBool(Name, "InitMax", WindowState == wsMaximized))
WindowState = wsMaximized;
else
WindowState = wsNormal;
}
catch(Exception* e)
{
}
delete SettingsFile;
}
TCustomIniFile* __fastcall TForm2::OpenIniFileInstance()
{
TCustomIniFile* temp;
switch (RadioGroup1->ItemIndex)
{
case 0: {
/* Registry mode selected: in HKEY_CURRENT_USER\Software\... */
temp = new TRegistryIniFile(String("Software\\") + Application->Title);
}
break;
case 1: {
/* Ini file mode selected */
temp = new TIniFile(ChangeFileExt(Application->ExeName, ".INI"));
}
break;
case 2: {
/* Memory based Ini file mode selected */
temp = new TMemIniFile(ChangeFileExt(Application->ExeName, ".INI"));
}
break;
default:
break;
}
return temp;
}
【问题讨论】:
-
第 76 行发生错误的位置在哪里?另请注意,
OpenIniFileInstance中的temp未初始化,当default-case 因某种原因被命中时,您将返回垃圾指针。 -
感谢您的回答。即使我删除了“默认”情况,我也会遇到同样的错误。我认为错误比初始化问题更深。
-
那更多的是关于第 76 行在哪里的问题;) 而关于
default-cases 的问题是,通常当它们被击中时,出现了问题。在您的情况下,当default被点击时,请考虑将返回什么值以及这是否是一种想要的行为。 -
我的代码中的第 76 行是
case 0:` temp = new TRegistryIniFile(String("Software\\") + Application->Title);` -
P.S:我从这个 [link]docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/… 得到这个代码,稍作修改。
标签: c++ file c++builder ini