错误:“表达式必须具有类类型” C++/CLI

发布于 2024-12-24 20:38:25 字数 568 浏览 0 评论 0原文

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

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);
    }   
}

Not sure why this won't compile. What sort of error am I making here and how do I fix it? I am trying to compile this code I found in an example but my compiler must have stricter settings than theirs or maybe a different version of compiler. The code should just open up a windows form and display some text.

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);
    }   
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无敌元气妹 2024-12-31 20:38:25

您为覆盖和访问方法编写了不正确的语法。

 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;
}

You've written incorrect syntax to override and access methods.

 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);
  }

and don't use void main().

[STAThreadAttribute]
int main()
{
     Application::Run(gcnew Form1());
     return 0;
}
人海汹涌 2024-12-31 20:38:25

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

The error is with Font, which is a class. The call expects a font, that is, a Font instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文