C++ Builder > __property with Setter(String) 导致错误 [E2034 无法将 'UnicodeString' 转换为 'float']
C++ Builder > __property with Setter(String) causes error [E2034 Cannot convert 'UnicodeString' to 'float']
我的环境
C++ Builder XE4 on Windows7 Pro (32bit)
我尝试将浮点值 属性 设为:
- __property 的浮点值
- setter 具有 [String] 参数,例如字符串为“1e-7”
- getter 以科学 E 记数法返回(例如 1e-7)
Unit2.h
//---------------------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <System.hpp>
class TMyClass {
private:
float FRealValue;
String GetRealValue();
void SetRealValue(String strValue);
public:
__property float RealValue = { read = GetRealValue, write = SetRealValue };
TMyClass() : RealValue(0.0) {};
};
#endif
Unit2.cpp
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
String TMyClass::GetRealValue()
{
return String().sprintf(L"%e", FRealValue);
}
void TMyClass::SetRealValue(String strValue)
{
FRealValue = strValue.ToDouble();
}
Unit1.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: //
TEdit *E_val1;
TButton *Button1;
TEdit *E_val2;
void __fastcall Button1Click(TObject *Sender);
private: //
public: //
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
E_val1->Text = L"1e-7";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMyClass *myInst = new TMyClass();
myInst->RealValue = E_val1->Text; // (1)
E_val2->Text = myInst->RealValue;
delete myInst;
}
//---------------------------------------------------------------------------
在Unit1.cpp的(1)中,我有错误:
Unit1.cpp(23): E2034 Cannot convert 'UnicodeString' to 'float'
问题:
浮点值 属性 不能使用 String 参数吗?
您不能让 属性 getter/setter 使用不同于 属性 要求的数据类型。
A getter for a float
属性 必须 return a float
作为输出:
float __fastcall TMyClass::GetRealValue()
A setter 对于 float
属性 必须将 float
作为输入:
void __fastcall TMyClass::SetRealValue(float aValue)
类型不能是 String
或 int
或任何其他数据类型。必须是 float
.
这是因为某种类型的 属性 实际上被翻译为对其 getter 和 setter 函数的调用。所以
float value = myClass->RealValue;
编译为:
float value = myClass->GetRealValue();
和
myClass->RealValue = 1.345;
编译为:
myClass->SetRealValue(1.345);
如果您想做您正在尝试的事情(输入和输出字符串),您必须将 属性 声明为 System::String
而不是 float
。
我的环境
C++ Builder XE4 on Windows7 Pro (32bit)
我尝试将浮点值 属性 设为:
- __property 的浮点值
- setter 具有 [String] 参数,例如字符串为“1e-7”
- getter 以科学 E 记数法返回(例如 1e-7)
Unit2.h
//---------------------------------------------------------------------------
#ifndef Unit2H
#define Unit2H
//---------------------------------------------------------------------------
#include <System.hpp>
class TMyClass {
private:
float FRealValue;
String GetRealValue();
void SetRealValue(String strValue);
public:
__property float RealValue = { read = GetRealValue, write = SetRealValue };
TMyClass() : RealValue(0.0) {};
};
#endif
Unit2.cpp
//---------------------------------------------------------------------------
#pragma hdrstop
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
String TMyClass::GetRealValue()
{
return String().sprintf(L"%e", FRealValue);
}
void TMyClass::SetRealValue(String strValue)
{
FRealValue = strValue.ToDouble();
}
Unit1.h
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: //
TEdit *E_val1;
TButton *Button1;
TEdit *E_val2;
void __fastcall Button1Click(TObject *Sender);
private: //
public: //
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
E_val1->Text = L"1e-7";
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMyClass *myInst = new TMyClass();
myInst->RealValue = E_val1->Text; // (1)
E_val2->Text = myInst->RealValue;
delete myInst;
}
//---------------------------------------------------------------------------
在Unit1.cpp的(1)中,我有错误:
Unit1.cpp(23): E2034 Cannot convert 'UnicodeString' to 'float'
问题: 浮点值 属性 不能使用 String 参数吗?
您不能让 属性 getter/setter 使用不同于 属性 要求的数据类型。
A getter for a float
属性 必须 return a float
作为输出:
float __fastcall TMyClass::GetRealValue()
A setter 对于 float
属性 必须将 float
作为输入:
void __fastcall TMyClass::SetRealValue(float aValue)
类型不能是 String
或 int
或任何其他数据类型。必须是 float
.
这是因为某种类型的 属性 实际上被翻译为对其 getter 和 setter 函数的调用。所以
float value = myClass->RealValue;
编译为:
float value = myClass->GetRealValue();
和
myClass->RealValue = 1.345;
编译为:
myClass->SetRealValue(1.345);
如果您想做您正在尝试的事情(输入和输出字符串),您必须将 属性 声明为 System::String
而不是 float
。