【发布时间】: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