【问题标题】:Visual Studio C++ ERR : Functions already have bodiesVisual Studio C++ 错误:函数已经有主体
【发布时间】:2023-03-12 19:59:01
【问题描述】:

只是一个警告,我是 C++ 新手和缺乏经验,所以如果我的语法真的很糟糕或者我遗漏了一些明显的东西,请原谅我。我正在尝试制作一个允许我从其中打开其他程序(如游戏启动器)的程序。

我不想在代码中定义程序,这样我就可以在不重新编译的情况下更改它。我正在使用一个 Visual Studio 生成的 Windows 窗体头文件,我对它进行了一些调整和一个主 cpp 文件。当我去编译时,我得到一个ERR_MESSAGE 说我的函数已经有了主体。这是我的代码:

CPP 脚本MyForm.cpp

#include "MyForm.h"
#include <iostream> // Debug (Remove once done)
#include <fstream> // File stream
#include <stdlib.h> // Execute files

using namespace std;
using namespace System;
using namespace System::Windows::Forms;

// Creating the virtual file arrays
char line1[];
char line2[];
char line3[];
char line4[];
char line5[];
char line6[];
char line7[];
char line8[];
char line9[];
char line10[];
char line11[];
char line12[];

namespace Loader {
    void loadProgram(int num) {
        char new_line[] = { '\n' };
        if (num == 1) {
            if (line2 != new_line) {
                system(line2);
            }
        }
        else if (num == 2) {
            if (line4 != new_line) {
                system(line4);
            }
        }
        else if (num == 3) {
            if (line6 != new_line) {
                system(line6);
            }
        }
        else if (num == 4) {
            if (line8 != new_line) {
                system(line8);
            }
        }
        else if (num == 5) {
            if (line10 != new_line) {
                system(line10);
            }
        }
        else if (num == 6) {
            if (line12 != new_line) {
                system(line12);
            }
        }
    }
}

[STAThreadAttribute]
int main() {
    // Assigning variables made in pre-processing
    const int lineLength = 255;
    if (!lineLength) {
        const int lineLength = 255;
    }
    unsigned int lineLengthUnsigned = lineLength;
    char line1[lineLength];
    char line2[lineLength];
    char line3[lineLength];
    char line4[lineLength];
    char line5[lineLength];
    char line6[lineLength];
    char line7[lineLength];
    char line8[lineLength];
    char line9[lineLength];
    char line10[lineLength];
    char line11[lineLength];
    char line12[lineLength];

    ifstream file("Programs.txt"); // Load programs list file (READ ONLY)

    // Odd lines are program names, even lines are program shortcuts
    file.getline(line1, lineLength);
    file.getline(line2, lineLength);
    file.getline(line3, lineLength);
    file.getline(line4, lineLength);
    file.getline(line5, lineLength);
    file.getline(line6, lineLength);
    file.getline(line7, lineLength);
    file.getline(line8, lineLength);
    file.getline(line9, lineLength);
    file.getline(line10, lineLength);
    file.getline(line11, lineLength);
    file.getline(line12, lineLength);

    // Below will run the window
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);
    FileOpener::MyForm form;
    Application::Run(%form);
}

头文件MyForm.h

#pragma once

#include "MyForm.cpp"

using namespace System;
using namespace System::Windows::Forms;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Data;
using namespace System::Drawing;

namespace FileOpener {

    /// <summary>
    /// Summary for MyForm
    /// </summary>
    public ref class MyForm : public System::Windows::Forms::Form
    {
    public:
        MyForm(void)
        {
            InitializeComponent();
            //
            //TODO: Add the constructor code here
            //
        }

    protected:
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        ~MyForm()
        {
            if (components)
            {
                delete components;
            }
        }
    private: System::Windows::Forms::Button^  button1;
    private: System::Windows::Forms::Button^  button2;
    private: System::Windows::Forms::Button^  button3;
    private: System::Windows::Forms::Button^  button4;
    private: System::Windows::Forms::Button^  button5;
    private: System::Windows::Forms::Button^  button6;
    protected:

    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(MyForm::typeid));
            this->button1 = (gcnew System::Windows::Forms::Button());
            this->button2 = (gcnew System::Windows::Forms::Button());
            this->button3 = (gcnew System::Windows::Forms::Button());
            this->button4 = (gcnew System::Windows::Forms::Button());
            this->button5 = (gcnew System::Windows::Forms::Button());
            this->button6 = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            //
            // button1
            //
            this->button1->Location = System::Drawing::Point(12, 12);
            this->button1->Name = L"button1";
            this->button1->Size = System::Drawing::Size(64, 64);
            this->button1->TabIndex = 0;
            this->button1->Text = L"button1";
            this->button1->UseVisualStyleBackColor = true;
            this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
            //
            // button2
            //
            this->button2->Location = System::Drawing::Point(82, 12);
            this->button2->Name = L"button2";
            this->button2->Size = System::Drawing::Size(64, 64);
            this->button2->TabIndex = 1;
            this->button2->Text = L"button2";
            this->button2->UseVisualStyleBackColor = true;
            this->button2->Click += gcnew System::EventHandler(this, &MyForm::button2_Click);
            //
            // button3
            //
            this->button3->Location = System::Drawing::Point(152, 12);
            this->button3->Name = L"button3";
            this->button3->Size = System::Drawing::Size(64, 64);
            this->button3->TabIndex = 2;
            this->button3->Text = L"button3";
            this->button3->UseVisualStyleBackColor = true;
            this->button3->Click += gcnew System::EventHandler(this, &MyForm::button3_Click);
            //
            // button4
            //
            this->button4->Location = System::Drawing::Point(12, 82);
            this->button4->Name = L"button4";
            this->button4->Size = System::Drawing::Size(64, 64);
            this->button4->TabIndex = 3;
            this->button4->Text = L"button4";
            this->button4->UseVisualStyleBackColor = true;
            this->button4->Click += gcnew System::EventHandler(this, &MyForm::button4_Click);
            //
            // button5
            //
            this->button5->Location = System::Drawing::Point(82, 82);
            this->button5->Name = L"button5";
            this->button5->Size = System::Drawing::Size(64, 64);
            this->button5->TabIndex = 4;
            this->button5->Text = L"button5";
            this->button5->UseVisualStyleBackColor = true;
            this->button5->Click += gcnew System::EventHandler(this, &MyForm::button5_Click);
            //
            // button6
            //
            this->button6->Location = System::Drawing::Point(152, 82);
            this->button6->Name = L"button6";
            this->button6->Size = System::Drawing::Size(64, 64);
            this->button6->TabIndex = 5;
            this->button6->Text = L"button6";
            this->button6->UseVisualStyleBackColor = true;
            this->button6->Click += gcnew System::EventHandler(this, &MyForm::button6_Click);
            //
            // MyForm
            //
            this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->BackColor = System::Drawing::Color::Black;
            this->BackgroundImage = (cli::safe_cast<System::Drawing::Image^>(resources->GetObject(L"$this.BackgroundImage")));
            this->ClientSize = System::Drawing::Size(228, 157);
            this->Controls->Add(this->button6);
            this->Controls->Add(this->button5);
            this->Controls->Add(this->button4);
            this->Controls->Add(this->button3);
            this->Controls->Add(this->button2);
            this->Controls->Add(this->button1);
            this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::FixedSingle;
            this->Icon = (cli::safe_cast<System::Drawing::Icon^>(resources->GetObject(L"$this.Icon")));
            this->Name = L"MyForm";
            this->Text = L"File Opener";
            this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
            this->ResumeLayout(false);

        }
    private: System::Void MyForm_Load(System::Object^  sender, System::EventArgs^  e) {
    }
    public: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
        Loader::loadProgram(1);
    }
    public: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
        Loader::loadProgram(2);
    }
    public: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
        Loader::loadProgram(3);
    }
    public: System::Void button4_Click(System::Object^  sender, System::EventArgs^  e) {
        Loader::loadProgram(4);
    }
    public: System::Void button5_Click(System::Object^  sender, System::EventArgs^  e) {
        Loader::loadProgram(5);
    }
    public: System::Void button6_Click(System::Object^  sender, System::EventArgs^  e) {
        Loader::loadProgram(6);
    }
};
}

#pragma endregion

再次,对于我的代码或这篇文章中任何值得关注的部分,我们深表歉意,因为这是我的第一篇文章。任何指针/提示将不胜感激!

【问题讨论】:

  • #include "MyForm.cpp" 通常不属于头文件。另外,这不是 C++ 而是 C++/CX 或 C++/CLI ,请适当标记
  • 谢谢,对不起,我是新手。我会改变它。我使用#include "MyForm.cpp" 调用'Loader::loadProgram(x);`,有更好的方法吗?

标签: c++-cli c++-cx


【解决方案1】:

通过使用 PeterT 的建议,我能够修复函数已经有实体错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    • 2012-10-25
    相关资源
    最近更新 更多