C++ 使用二传器(字符串)的生成器> __property导致错误 [E2034 无法将'UnicodeString'转换为"浮点数"]



我的环境

C++ Builder XE4 on Windows7 Pro (32bit)

我尝试将浮点值属性设置为:

  • 浮点值__property
  • 具有 [String] 参数的 setter,例如字符串是"1e-7">
  • 以科学电子符号返回的 getter (例如 1e-7(

单位2.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

单元2.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();
}

单位1.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

单元1.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(中,我有错误:

单元1.cpp(23(: E2034 无法将"UnicodeString"转换为"float">

问题: 是否可以将字符串参数用于浮点值属性?

不能让属性 getter/setter 使用与属性所需的数据类型不同的数据类型。

float属性的 getter 必须返回一个float作为输出:

float __fastcall TMyClass::GetRealValue()

float属性的 setter 必须将float作为输入:

void __fastcall TMyClass::SetRealValue(float aValue)

该类型不能是Stringint或任何其他数据类型。它一定是float.

这是因为某种类型的属性实际上被转换为对其 getter 和 setter 函数的调用。所以

float value = myClass->RealValue;

编译为:

float value = myClass->GetRealValue();

myClass->RealValue = 1.345;

编译为:

myClass->SetRealValue(1.345);

如果要执行正在尝试的操作(字符串输入和输出(,则必须将属性声明为System::String而不是float

最新更新