我在这个 class 构造函数上做错了什么?
What I'm doing wrong on this class constructor?
Visual Studio 给了我下一个错误:'Error: expected an identifier' 在我的 .cpp 文件中,我在 .h 文件中声明了它定义的函数。
UI_Main.h:
#pragma once
namespace ProcessMonitoring {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for UI_Main
/// </summary>
public ref class UI_Main : public System::Windows::Forms::Form
{
public: UI_Main(void);
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~UI_Main()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(UI_Main::typeid));
this->SuspendLayout();
//
// UI_Main
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Icon = (cli::safe_cast<System::Drawing::Icon^ >(resources->GetObject(L"$this.Icon")));
this->IsMdiContainer = true;
this->Name = L"UI_Main";
this->Text = L"PMonitoring";
this->ResumeLayout(false);
}
#pragma endregion
};
}
UI_Main.cpp(UI_Main 构造函数出错):
#include "UI_Main.h"
namespace ProcessMonitoring{
ProcessMonitoring::UI_Main(void){
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
};
这一定是一个非常简单的失败,但我找不到它...提前谢谢你!
尝试调试后的错误是:
* error C2062: type 'void' unexpected
* error C2143: syntax_error: missing ';' before '{'
* error C2447: '{': missing function header (old-style formal list?)
* IntelliSense: expected a ';'
* IntelliSense: expected an identifier
这个
namespace ProcessMonitoring{
ProcessMonitoring::UI_Main(void){
应该是
namespace ProcessMonitoring
{
UI_Main::UI_Main(void)
{
您已经提供了命名空间名称,因此您不必再次使用它进行限定。但是您必须为 class 之外的构造函数定义使用完全限定名称,这意味着要写两次 class 名称。
你的语法:
ProcessMonitoring::UI_Main(identifier);
为变量声明,类型为::ProcessMonitoring::UI_Main
,变量名为identifier
,括号多余。但是 void
是关键字,不是合法的变量名,因此编译器拒绝它并出现您看到的错误。
Visual Studio 给了我下一个错误:'Error: expected an identifier' 在我的 .cpp 文件中,我在 .h 文件中声明了它定义的函数。
UI_Main.h:
#pragma once
namespace ProcessMonitoring {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for UI_Main
/// </summary>
public ref class UI_Main : public System::Windows::Forms::Form
{
public: UI_Main(void);
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~UI_Main()
{
if (components)
{
delete components;
}
}
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(UI_Main::typeid));
this->SuspendLayout();
//
// UI_Main
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 261);
this->Icon = (cli::safe_cast<System::Drawing::Icon^ >(resources->GetObject(L"$this.Icon")));
this->IsMdiContainer = true;
this->Name = L"UI_Main";
this->Text = L"PMonitoring";
this->ResumeLayout(false);
}
#pragma endregion
};
}
UI_Main.cpp(UI_Main 构造函数出错):
#include "UI_Main.h"
namespace ProcessMonitoring{
ProcessMonitoring::UI_Main(void){
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
};
这一定是一个非常简单的失败,但我找不到它...提前谢谢你!
尝试调试后的错误是:
* error C2062: type 'void' unexpected
* error C2143: syntax_error: missing ';' before '{'
* error C2447: '{': missing function header (old-style formal list?)
* IntelliSense: expected a ';'
* IntelliSense: expected an identifier
这个
namespace ProcessMonitoring{
ProcessMonitoring::UI_Main(void){
应该是
namespace ProcessMonitoring
{
UI_Main::UI_Main(void)
{
您已经提供了命名空间名称,因此您不必再次使用它进行限定。但是您必须为 class 之外的构造函数定义使用完全限定名称,这意味着要写两次 class 名称。
你的语法:
ProcessMonitoring::UI_Main(identifier);
为变量声明,类型为::ProcessMonitoring::UI_Main
,变量名为identifier
,括号多余。但是 void
是关键字,不是合法的变量名,因此编译器拒绝它并出现您看到的错误。