C ++ Builder>带有Setter(String)的__property导致错误[E2034无法转换' UnicodeString'到'浮动']

时间:2017-07-27 04:50:52

标签: c++builder

我的环境

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

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

  • __浮动值的属性
  • setter有[String]参数,其中字符串是" 1e-7"例如
  • 以科学E-notation(例如1e-7)返回的getter

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无法转换&#39; UnicodeString&#39;到&#39;浮动&#39;

问题: 是不是可以将String参数作为浮点值属性?

1 个答案:

答案 0 :(得分:2)

您不能让属性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