【问题标题】:Namespace with 2 forms具有 2 种形式的命名空间
【发布时间】:2015-06-18 20:54:43
【问题描述】:

我有命名空间“客户端”,表单 MainWindow 和表单 MyForm

MainWindow 创建 MyForm。

MainWindow.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> MainWindow.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 有公共方法void Print () { cout << "HEY" << endl; }

在 MyForm.h 我这样做:f->Print();

【问题讨论】:

  • #include "MainWindow.h"?假设在那里定义了MainWindow...
  • @ArnonZilca 更新帖
  • 首先只是为了确保您将#include... 放在#pragma once 之后。除此之外,您能否粘贴“MainWindow.h”(至少在 MainWindow 类定义之前)?
  • 我在这里拍摄空白...可能是您在定义任何类 (explanation here) 之后不知何故缺少;
  • @ArnonZilca 不,bcz 如果我删除此行 MainWindow ^f; 工作正常

标签: winforms visual-studio-2010 namespaces c++-cli


【解决方案1】:

对于习惯于较新语言的程序员来说,这往往有点粗糙。但这是正常的问题,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 文件。不要惊慌,这很正常。

【讨论】:

  • 是的!现在可以了,非常感谢!它确实增加了很多舞蹈(但如果这不起作用,那就更好了))
【解决方案2】:

您似乎忘记包含定义 Client::MainWindow 的标头

【讨论】:

  • MyForm 的标题?是的,没有包含标题,但如果我添加它,错误仍然存​​在。
  • 好吧,看来 MainWindow 是 MyForm 的父级。因此,您可以在 MyForm.h 中使用 Client::MainWindow 的前向声明(并在 MyForm.cpp 中添加 #include "MainWindow.h"。
  • 我的问题中第二个错误示例中的错误
  • 如果你把namespace Client { class MainWindow; } 放在MyForm.h 的声明之前呢? (MyForm.h 中没有#include "MainWindow.h")
  • 1>c:\users\user\desktop\testlist\client\MainWindow.h(18): error C3816: 'class Client::MainWindow' was previously declared or defined with a different managed modifier 1> c:\users\user\desktop\testlist\client\MyForm.h(4) : see declaration of 'Client::MainWindow' 1> MainWindow.cpp
猜你喜欢
  • 2011-06-03
  • 1970-01-01
  • 1970-01-01
  • 2014-09-17
  • 1970-01-01
  • 2011-12-01
  • 1970-01-01
  • 2019-03-01
  • 2013-02-18
相关资源
最近更新 更多