我在这个类构造函数上做错了什么?

时间:2017-11-10 13:11:09

标签: .net visual-studio constructor c++-cli

Visual Studio为我提供了下一个错误:'错误:我的.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

1 个答案:

答案 0 :(得分:1)

namespace ProcessMonitoring{

    ProcessMonitoring::UI_Main(void){

应该是

namespace ProcessMonitoring
{
    UI_Main::UI_Main(void)
    {

您已经提供了命名空间名称,因此您无需再次使用它。但是你必须为类外部的构造函数定义使用完全限定名,这意味着要两次编写类名。

您拥有的语法:

ProcessMonitoring::UI_Main(identifier);

是变量声明,其中类型为::ProcessMonitoring::UI_Main,变量名称为identifier,括号是多余的。但是void是一个关键字,而不是一个合法的变量名,所以编译器会用你看到的错误拒绝它。