【问题标题】:Error: "Expression must have class type" C++/CLI错误:“表达式必须具有类类型”C++/CLI
【发布时间】:2012-01-05 08:47:35
【问题描述】:

不知道为什么这不会编译。我在这里犯了什么样的错误,我该如何解决?我正在尝试编译在示例中找到的此代码,但我的编译器必须具有比他们更严格的设置,或者可能是不同版本的编译器。代码应该只是打开一个窗体并显示一些文本。

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;

public ref class MyForm : Form
{
public:

MyForm ()
    {
    Text = "Windows Forms Demo";
    }

void Main ()
    {
    Application.Run (gcnew MyForm());
    }

protected:
 void OnPaint (PaintEventArgs e)
    {

    e.Graphics.DrawString ("Hello, world", Font,
        gcnew SolidBrush (Color.Black), ClientRectangle);
    }   
}

【问题讨论】:

  • Sorry error is on line "e.Graphics.DrawString" e 被突出显示,它说 Expression must have a class type
  • 你的编译器应该给你一些行信息,以及详细的错误信息。尝试使用编译器启用所有警告。

标签: .net c++-cli compiler-errors


【解决方案1】:

您向override 和访问方法编写了错误的语法。

 virtual void OnPaint(PaintEventArgs^ e) override
  {
    Form::OnPaint(e);
    e->Graphics->DrawString("Hello, world", gcnew System::Drawing::Font("Arial",20),  gcnew SolidBrush (Color::Black), ClientRectangle);
  }

不要使用void main()

[STAThreadAttribute]
int main()
{
     Application::Run(gcnew Form1());
     return 0;
}

【讨论】:

    【解决方案2】:

    错误在于Font,它是一个类。该调用需要一个字体,即Font 实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 2017-07-28
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      相关资源
      最近更新 更多