我对 C++ 很陌生,我正在尝试使用 Visual C++ Express 2010 创建一个简单的表单应用程序。当它只是一个头文件和一个 .cpp 文件并且它在我的计算机上完美运行时,我让它编译没有错误美好的。当我试图给我的朋友编译的 .exe 运行时,它没有运行。他有 .NET 框架 4.5 和 Visual C++ 2010 可再发行包,但它仍然拒绝工作。他说它根本不会启动(我不知道它是否给他一个错误消息)。程序只有一个改变标签的按钮。我在这里扯头发,因为除了我自己的计算机之外,我似乎无法让它在任何其他计算机上工作。请帮助我理解为什么这不会在另一台计算机上运行。这可能是我犯的错误,所以代码在下面。如您所见,我已将其变成 3 。cpp 文件,看看是否可行。这里有很多无意义的代码,但它仍然编译得很好。
//Source2.cpp
#pragma once
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 Form1
/// </summary>
public ref class Header1 : public System::Windows::Forms::Form
{
public:
Header1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Header1()
{
if (components)
{
delete components;
}
}
public: System::Windows::Forms::Button^ button1;
System::Windows::Forms::Label^ label1;
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)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(13, 13);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Header1::button1_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(13, 43);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(46, 17);
this->label1->TabIndex = 1;
this->label1->Text = L"label1";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(282, 253);
this->Controls->Add(this->label1);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
this->label1->Text = "works";
}
};
^^请注意,这最初是一个名为 Header1.h 的头文件
//Source1.cpp
#include "stdafx.h"
#include "Source2.cpp"//used to include Header1.h
public class runpr
{
public:
runpr()
{
create();
}
~runpr();
private:
void create()
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Header1());
//^^all of this was originally in the main function
}
};
该文件毫无意义,因为它所做的所有事情都可以在主函数中完成,或者该类甚至可以在主项目文件中。我只是尝试了它,看看它是否会工作,但无济于事。
// Testing123.cpp : main project file.
#include "stdafx.h"
#include "Source1.cpp"
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
runpr* start = new runpr;
//creates an instance of runpr class
return 0;
}
同样,任何帮助将不胜感激。对不起,如果这个问题太长而且太愚蠢了