2014-05-15 54 views
-1

在BDS XE6我试图放置文本使用Canvas-> FillText。 我有一些常量声明的问题,我无法克服这个问题。Canvas FillText非常量编译器错误

TRect *Rect = new TRect(0, 0, 100, 30); 
Canvas->FillText(*Rect, "Hello", false, 100, TFillTextFlags() << TFillTextFlag::RightToLeft, TTextAlign::Center, TTextAlign::Center); 

我得到的编译器错误:

[bcc32 Error] MyForm.cpp(109): E2522 Non-const function _fastcall TCanvas::FillText(const TRectF &,const UnicodeString,const bool, const float,const TFillTextFlags,const TTextAlign,const TTextAlign) called for const object 
    Full parser context 
    LavEsiti.cpp(107): parsing: void _fastcall TMyForm::MyGridDrawColumnCell(TObject *,const TCanvas *,const TColumn *,const TRectF &,const int,const TValue &,const TGridDrawStates) 

我希望得到我的错误一些信息。提前致谢。

回答

0

问题通过调用函数的const_cast选项解决:

const_cast<TCanvas*>(Canvas)->FillText(*Rect, "Hello", false, 100, TFillTextFlags() << TFillTextFlag::RightToLeft, TTextAlign::Center, TTextAlign::Center); 
1

TRect *Rect = new TRect(0, 0, 100, 30);

你并不需要动态分配TRect,使用基于堆栈的实例,而不是:

TRect Rect(0, 0, 100, 30); 
Canvas->FillText(Rect, ...); 

或者,使用Rect()函数根本没有变量:

Canvas->FillText(Rect(0, 0, 100, 30), ...); 

I get the compiler error: [bcc32 Error] MyForm.cpp(109): E2522 Non-const function _fastcall TCanvas::FillText(const TRectF &,const UnicodeString,const bool, const float,const TFillTextFlags,const TTextAlign,const TTextAlign) called for const object

Delphi(写入VCL)没有const的概念 - 类的方法的概念,就像C++一样。 OnDrawColumnCell事件的Canvas参数声明为const(我不知道为什么),但TCanvas::FillText()方法未声明为const。这就是为什么你得到Non-const function ... called for const object错误。德尔福没有问题,但C++。

正如您已经发现的,您可以将const_cast错误带走,但这不仅仅是一种解决方案。事件处理程序不应该首先声明一个对象指针为const,这是对首先编写该事件的人的监督。