C ++ Windows应用程序表单无法进行前向声明(C++ Windows Application Form Cannot make Forward Declaration)

编程入门 行业动态 更新时间:2024-10-28 20:29:19
C ++ Windows应用程序表单无法进行前向声明(C++ Windows Application Form Cannot make Forward Declaration)

我在Windows应用程序表单中使用Visual C++ 2010 :

#pragma once namespace MyProgram { using namespace System; using namespace System::IO; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; ref class Design; //Forward Declariong of Class Design /// <summary> /// Summary for Form1 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { private: Design Enviroment; //Declaring object of class: Design public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // } }; public ref class Design { private: String^ Color; public: Design() { //TODO: Add the Constructor code here } };

我得到error: MyProgram::Form1::Enviroment'使用未定义的类' MyProgram::Design '

如果我切换定义顺序,它将编译没有错误,但在Windows应用程序窗体中,类Form1总是必须首先...那么,我的前向声明是错误的吗?

I am using Visual C++ 2010, in Windows Application Form:

#pragma once namespace MyProgram { using namespace System; using namespace System::IO; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; ref class Design; //Forward Declariong of Class Design /// <summary> /// Summary for Form1 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { private: Design Enviroment; //Declaring object of class: Design public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // } }; public ref class Design { private: String^ Color; public: Design() { //TODO: Add the Constructor code here } };

And i get the error: MyProgram::Form1::Enviroment' uses undefined class 'MyProgram::Design'

If I switch the definition order, it will compile with no errors, but in windows application form the class Form1 always has to be first... So, is my forward declaration wrong?

最满意答案

由于您声明了一个类型为Design的变量(Environment),因此当时编译器必须可以使用完整类型。

通过重新排序声明,您将向编译器提供完整类型。

Since you are declaring a variable (Environment) of type Design the complete type has to be available to the compiler at that time.

By re-ordering your declarations, you are providing the complete type to the compiler.

更多推荐

本文发布于:2023-07-22 20:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1223274.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:表单   应用程序   前向   声明   Windows

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!