gpt4 book ai didi

winforms - 具有 2 个表单的命名空间

转载 作者:行者123 更新时间:2023-12-04 16:59:20 25 4
gpt4 key购买 nike

我有命名空间“Client”,表单 MainWindow 和表单 MyForm

MainWindow 创建 MyForm。

主窗口.h

#pragma once

namespace Client {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

public ref class MainWindow : public System::Windows::Forms::Form
{
public:
MainWindow(void)
{
InitializeComponent();
}
....
....
....
}

在 MyForm.h 我这样写:
#pragma once

namespace Client {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

public ref class MyForm : public System::Windows::Forms::Form
{
private:
MainWindow ^f; //this is my problem
public:
MyForm(void)
{
InitializeComponent();
}
......
......
......
}

编译后,我在行 MainWindow ^f; 中出现此错误:
1>c:\users\user\desktop\testlist\client\MyForm.h(17): error C2143: syntax error : missing ';' before '^'
1>c:\users\user\desktop\testlist\client\MyForm.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> MyForm.cpp

如果我写这个 Client::MainWindow ^f; :
1>c:\users\user\desktop\testlist\client\MyForm.h(17): error C2039: 'MainWindow' : is not a member of 'Client'
1>c:\users\user\desktop\testlist\client\MyForm.h(17): error C2143: syntax error : missing ';' before '^'
1>c:\users\user\desktop\testlist\client\MyForm.h(17): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> MainWindow.cpp

1 错误 - 表单是客户端的成员,为什么?

如果添加 #include "MainWindow.h" ,错误在 MainWindow ^f; :
1>c:\users\user\desktop\testlist\client\MyForm.h(19): error C2143: syntax error : missing ';' before '^'
1>c:\users\user\desktop\testlist\client\MyForm.h(19): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1> MainWindow.cpp

我该如何解决这个问题?

________________________________________ ArnonZilca 的更新

Myform - 它是一个 ref 类,所以我使用 ref struct 而不是 mreoer myvar;我写 mreoer ^myvar; mreoer ^myvar; 中的错误:
1>c:\users\user\desktop\testlist\client\MyForm.h(20): error C2143: syntax error : missing ';' before '^'
1>c:\users\user\desktop\testlist\client\MyForm.h(20): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

1>c:\users\user\desktop\client\client\MyForm.h(155): error C2227: left of '->Print' must point to class/struct/union/generic type
1>主窗口.cpp

_________________________________更新

所以在 Myform.h 我写这个:
 #pragma once

namespace Client {
ref class MainWindow;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

public ref class MyForm : public System::Windows::Forms::Form
{
private:
MainWindow ^f; //this is my problem
public:
MyForm(void)
{
InitializeComponent();
}
......
......
......
}

如果我在 MyForm.h 中使用这个变量 (^f),我在使用它的地方会出现这个错误:
\users\user\desktop\client\client\MyForm.h(155): error C2027: use of undefined type 'Client::MainWindow'
1> c:\users\user\desktop\client\client\MyForm.h(8) : see declaration of 'Client::MainWindow'

MainWindow 有公共(public)方法 void Print () { cout << "HEY" << endl; }
在 MyForm.h 我这样做: f->Print();

最佳答案

对于习惯于较新语言的程序员来说,这往往有点粗糙。但这是正常的一种问题,C++/CLI 继承了 C++ 语言的编译模型。它是一个单程编译器,在使用它们之前必须知道所有定义。一个可以追溯到上个世纪的模型,当时 64KB 的 RAM 可以装进鞋盒,而且只需要一条胳膊和三条腿。

从技术上讲,它并没有那么糟糕,C++ 更像是一个 1.5 pass 编译器。您可以提前引用内联函数定义中的类成员。这并不能完全帮助诊断此类问题:)

但是你必须在这里做 C++ 舞蹈,你的 MyForm.h 文件可以包含一个前向声明,ref class MainWindow;搞定。但是任何取消引用 f 成员的代码必须只出现在 MyForm.cpp 文件中。该 .cpp 文件可以 #include MyForm.h 和 MainWindow.h,因此所有类型定义都可用。是的,这确实意味着您可能必须将设计者添加的方法从 .h 文件移动到 .cpp 文件。不要 panic ,这是正常的。

关于winforms - 具有 2 个表单的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29607152/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com