私の環境C++ Builderの>セッター(String)で__propertyは、エラーが発生[E2034は 'フロート' に 'UnicodeStringに' を変換できません]
C++ Builder XE4 on Windows7 Pro (32bit)
私はとフロートの値を持つプロパティを作ってみました:
-
浮動小数点値の
- __property
- セッター文字列である[文字列]引数を有する "1E-7" は、例えば科学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;
}
//---------------------------------------------------------------------------
(1)Unit1.cppの、私が持っているエラーで
:
Unit1.cpp(23): E2034 Cannot convert 'UnicodeString' to 'float'
質問: は、それがfloat値プロパティの文字列引数を取ることができないのですか?
これは技術的には真ですが、C++ Builderユーザーにとっては少し誤解を招く可能性があります。 C++の 'System :: String'クラスは入力として' double'を受け入れるコンストラクタを持っていますので、 'SetRealValue()'が 'String'を期待するとき' myClass-> SetRealValue(1.345) 'は有効です。 C++コンパイラは、実際のセッタシグネチャを見て、プロパティの型に関係なく有効な入力を許可することができるので、 'myInst-> RealValue = E_val1-> Text'(' myInst-> SetRealValue(E_val1-> Text) ') )* 'SetRealValue()'が 'String'を取るときに*有効になります。しかし、コンパイラはプロパティの型を正しく尊重し、 'String'の入力を拒否します。 –
' String'引数を持つgetterまたはsetterは、 'float'型のプロパティのゲッタまたはセッタとして使用できません。 Askerが文字列を使用してプロパティを取得または設定する場合、プロパティを単に 'String'として宣言できます。これが内部的にどのように格納または処理されるかは重要ではありません。 –