当前位置: 动力学知识库 > 问答 > 编程问答 >

visual c++ - Form2 Not Opening?

问题描述:

I am making a Video game using microsoft Visual c++, I am coding all of this in CLR language, But I this weird problem....

For some reason Whenever I try and open form 2 with Form 1 all I get are these errors...

 *1>------ Build started: Project: Retaliation, Configuration: Debug Win32 ------

1> Form2.cpp

1>c:\users\devon\documents\visual studio 2010\projects\retaliation\retaliation\Form1.h(431): error C2065: 'Form2' : undeclared identifier

1>c:\users\devon\documents\visual studio 2010\projects\retaliation\retaliation\Form1.h(431): error C2065: 'f2' : undeclared identifier

1>c:\users\devon\documents\visual studio 2010\projects\retaliation\retaliation\Form1.h(431): error C2061: syntax error : identifier 'Form2'

1>c:\users\devon\documents\visual studio 2010\projects\retaliation\retaliation\Form1.h(432): error C2065: 'f2' : undeclared identifier

1>c:\users\devon\documents\visual studio 2010\projects\retaliation\retaliation\Form1.h(432): error C2227: left of '->Show' must point to class/struct/union/generic type

1> type is ''unknown-type''

1> Retaliation.cpp

1> Generating Code...

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========*

Here is my code for form 1...

 #pragma once

#include "stdAfx.h"

#include "Form2.h"

#include "Form1.h"

namespace Retaliation {

using namespace System;

using namespace System::ComponentModel;

using namespace System::Collections;

using namespace System::Windows::Forms;

using namespace System::Data;

using namespace System::Drawing;

using namespace System::IO;

using namespace std;

/// <summary>

/// Summary for Form1

/// </summary>

public ref class Form1 : public System::Windows::Forms::Form

{

public:

String^ CharName;

String^ Race;

String^ Gender;

String^ Specialty;

String^ Friend;

int YesorNo;

int ExitChoice;

int TimerValue;

int MonsterN;

int EnemyHp;

int Dm;

int Energy;

int Action;

int Rage;

int MaxMana;

int MaxRage;

int MaxEnergy;

int Save;

// 1 = Earth pony 2 = Pegasus 3 = unicorn

int Lv;

int XpR;

int Xp;

int A;

int D;

int Hp;

int MaxHp;

int Magic;

int Mana;

int Agility;

int Gold;

String ^Enpowerment;

// 1 = Friendship is Magic, 2 = Music is Magic, 3 = Darkness is Magic, 4 = Light is Magic, 5 = Chaos is Magic

// End of leveling thread

int Checkpoint;

int FriendNameN;

int SavePoint;

int MenuChoice;

int RightMenuChoice;

int RestartFile;

int Answer;

int EnemyA;

int EnemyD;

int EnemyLv;

int EnemyMana;

int EnemyMagic;

int EnemyAgility;

int Random;

String ^ EName;

int Damage;

int Survivors;

int SavedSurvivors;

private: System::Windows::Forms::TextBox^ textBox1;

private: System::Windows::Forms::Timer^ timer1;

public:

int MainActions;

Form1(void)

{

InitializeComponent();

//

//TODO: Add the constructor code here

//

}

protected:

/// <summary>

/// Clean up any resources being used.

/// </summary>

~Form1()

{

if (components)

{

delete components;

}

}

private: System::Windows::Forms::PictureBox^ pictureBox1;

protected:

private: System::Windows::Forms::Button^ button1;

private: System::Windows::Forms::Button^ button2;

private: System::Windows::Forms::Button^ button3;

private: System::Windows::Forms::PictureBox^ pictureBox2;

private: System::Windows::Forms::Label^ label1;

private: System::Windows::Forms::Label^ label2;

private: System::Windows::Forms::Button^ button4;

private: System::Windows::Forms::Button^ button5;

private: System::Windows::Forms::PictureBox^ pictureBox3;

private: System::ComponentModel::IContainer^ components;

private:

/// <summary>

/// Required designer variable.

/// </summary>

#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->components = (gcnew System::ComponentModel::Container());

System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));

this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());

this->button1 = (gcnew System::Windows::Forms::Button());

this->button2 = (gcnew System::Windows::Forms::Button());

this->button3 = (gcnew System::Windows::Forms::Button());

this->pictureBox2 = (gcnew System::Windows::Forms::PictureBox());

this->label1 = (gcnew System::Windows::Forms::Label());

this->label2 = (gcnew System::Windows::Forms::Label());

this->button4 = (gcnew System::Windows::Forms::Button());

this->button5 = (gcnew System::Windows::Forms::Button());

this->pictureBox3 = (gcnew System::Windows::Forms::PictureBox());

this->textBox1 = (gcnew System::Windows::Forms::TextBox());

this->timer1 = (gcnew System::Windows::Forms::Timer(this- >components));

(cli::safe_cast<System::ComponentModel::ISupportInitialize^ > (this->pictureBox1))->BeginInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^ > (this->pictureBox2))->BeginInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox3))->BeginInit();

this->SuspendLayout();

//

// pictureBox1

//

this->pictureBox1->Image = (cli::safe_cast<System::Drawing::Image^ >(resources->GetObject(L"pictureBox1.Image")));

this->pictureBox1->Location = System::Drawing::Point(0, -3);

this->pictureBox1->Name = L"pictureBox1";

this->pictureBox1->Size = System::Drawing::Size(600, 295);

this->pictureBox1->TabIndex = 0;

this->pictureBox1->TabStop = false;

//

// button1

//

this->button1->Location = System::Drawing::Point(13, 230);

this->button1->Name = L"button1";

this->button1->Size = System::Drawing::Size(127, 43);

this->button1->TabIndex = 1;

this->button1->Text = L"Start Game";

this->button1->UseVisualStyleBackColor = true;

this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);

//

// button2

//

this->button2->Location = System::Drawing::Point(313, 212);

this->button2->Name = L"button2";

this->button2->Size = System::Drawing::Size(120, 42);

this->button2->TabIndex = 2;

this->button2->Text = L"New Game";

this->button2->UseVisualStyleBackColor = true;

this->button2->Visible = false;

this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);

//

// button3

//

this->button3->Location = System::Drawing::Point(143, 212);

this->button3->Name = L"button3";

this->button3->Size = System::Drawing::Size(120, 42);

this->button3->TabIndex = 3;

this->button3->Text = L"Load Game";

this->button3->UseVisualStyleBackColor = true;

this->button3->Visible = false;

this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);

//

// pictureBox2

//

this->pictureBox2->BackColor = System::Drawing::Color::Transparent;

this->pictureBox2->BackgroundImage = (cli::safe_cast<System::Drawing::Image^ >(resources- >GetObject(L"pictureBox2.BackgroundImage")));

this->pictureBox2->Location = System::Drawing::Point(0, -3);

this->pictureBox2->Name = L"pictureBox2";

this->pictureBox2->Size = System::Drawing::Size(86, 81);

this->pictureBox2->TabIndex = 4;

this->pictureBox2->TabStop = false;

this->pictureBox2->Visible = false;

//

// label1

//

this->label1->AutoSize = true;

this->label1->BackColor = System::Drawing::Color::Transparent;

this->label1->Location = System::Drawing::Point(92, -3);

this->label1->Name = L"label1";

this->label1->Size = System::Drawing::Size(22, 13);

this->label1->TabIndex = 5;

this->label1->Text = L" ";

this->label1->Visible = false;

//

// label2

//

this->label2->BackColor = System::Drawing::Color::Transparent;

this->label2->Location = System::Drawing::Point(358, 87);

this->label2->Name = L"label2";

this->label2->Size = System::Drawing::Size(199, 98);

this->label2->TabIndex = 9;

this->label2->Text = L"None";

this->label2->Visible = false;

//

// button4

//

this->button4->Location = System::Drawing::Point(463, 13);

this->button4->Name = L"button4";

this->button4->Size = System::Drawing::Size(75, 23);

this->button4->TabIndex = 12;

this->button4->Text = L"Yes";

this->button4->UseVisualStyleBackColor = true;

this->button4->Visible = false;

this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);

//

// button5

//

this->button5->Location = System::Drawing::Point(463, 43);

this->button5->Name = L"button5";

this->button5->Size = System::Drawing::Size(75, 23);

this->button5->TabIndex = 13;

this->button5->Text = L"No";

this->button5->UseVisualStyleBackColor = true;

this->button5->Visible = false;

this->button5->Click += gcnew System::EventHandler(this, &Form1::button5_Click);

//

// pictureBox3

//

this->pictureBox3->BackColor = System::Drawing::Color::Transparent;

this->pictureBox3->Location = System::Drawing::Point(13, 84);

this->pictureBox3->Name = L"pictureBox3";

this->pictureBox3->Size = System::Drawing::Size(165, 208);

this->pictureBox3->TabIndex = 14;

this->pictureBox3->TabStop = false;

this->pictureBox3->Visible = false;

//

// textBox1

//

this->textBox1->Location = System::Drawing::Point(463, 212);

this->textBox1->Name = L"textBox1";

this->textBox1->Size = System::Drawing::Size(100, 20);

this->textBox1->TabIndex = 15;

this->textBox1->Visible = false;

this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged_1);

//

// timer1

//

this->timer1->Interval = 1000;

this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);

//

// Form1

//

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

this->ClientSize = System::Drawing::Size(598, 285);

this->Controls->Add(this->textBox1);

this->Controls->Add(this->pictureBox3);

this->Controls->Add(this->button5);

this->Controls->Add(this->button4);

this->Controls->Add(this->label2);

this->Controls->Add(this->label1);

this->Controls->Add(this->pictureBox2);

this->Controls->Add(this->button3);

this->Controls->Add(this->button2);

this->Controls->Add(this->button1);

this->Controls->Add(this->pictureBox1);

this->Name = L"Form1";

this->Text = L"Retaliation";

(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->EndInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox2))->EndInit();

(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox3))->EndInit();

this->ResumeLayout(false);

this->PerformLayout();

}

#pragma endregion

public:int MoviePlay;

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

//

this -> button2 -> Visible = true;

this -> button3 -> Visible = true;

this -> button1 -> Visible = false;

this -> pictureBox1 -> Load("Derpy.jpg");

//

}

private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {

if (YesorNo == 0){

this -> button4 -> Visible = true;

this -> button2 -> Visible = false;

this -> button5 -> Visible = true;

this -> button3 -> Visible = false;

this -> label2 -> Visible = true;

this -> label2 -> Text = "Are you Sure?";

this -> YesorNo = 1;

}

else if (YesorNo == 5)

{

Gender = "Female";

this -> label2 -> Text = "What is your Race??";

this -> button4 -> Text = "Earth Pony";

this -> button5 -> Text = "Unicorn";

this -> button2 -> Visible = true;

this -> button2 -> Text = "Pegasus";

YesorNo = 6;

}

else if (YesorNo == 6)

{

Race = "Pegasus";

this -> button2 -> Visible = false;

this -> button4 -> Visible = false;

this -> button5 -> Visible = false;

label2 -> Text = "Thank You For Playing!!";

YesorNo = 7;

this -> timer1 -> Start();

}

}

private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {

}

private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {

if (YesorNo == 1)

{

this -> label2 -> Text = "To Make your Pony Character, See PonyMakin.txt in your Retaliation Folder, Your pony should have a invisible background... If not check your background color For your pony!";

button4 -> Text = "Show Pony";

YesorNo = 2;

}

else if ( YesorNo == 2)

{

this -> pictureBox3 -> Visible = true;

this -> pictureBox3 -> Load("Outline Body Model.jpg");

this -> pictureBox2 -> Visible = true;

this -> pictureBox2 -> Load("OutlineModel.jpg");

YesorNo = 3;

this -> textBox1 -> Visible = true;

this -> textBox1 -> Text = "Name?";

this -> button5 -> Visible = false;

this -> button4 -> Visible = false;

this -> label2 -> Visible = false;

}

else if (YesorNo == 5)

{

Gender = "Male";

this -> label2 -> Text = "What is your Race??";

this -> button4 -> Text = "Earth Pony";

this -> button5 -> Text = "Unicorn";

this -> button2 -> Visible = true;

this -> button2 -> Text = "Pegasus";

YesorNo = 6;

}

else if (YesorNo == 6)

{

Race = "Earth_Pony";

this -> button2 -> Visible = false;

this -> button4 -> Visible = false;

this -> button5 -> Visible = false;

label2 -> Text = "Thank You For Playing!!";

YesorNo = 7;

this -> timer1 -> Start();

}

}

private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) {

if (YesorNo == 1 || YesorNo == 2)

{

Application::Exit();

}

else if (YesorNo == 4)

{

this -> textBox1 -> Visible = false;

this -> label2 -> Visible = true;

this -> label2 -> Text = "What Is Your Gender?";

this -> button4 -> Text = "Male";

this -> button5 -> Text = "Female";

this -> button4 -> Visible = true;

this -> button5 -> Visible = true;

YesorNo = 5;

}

else if (YesorNo == 5)

{

Gender = "Female";

this -> label2 -> Text = "What is your Race??";

this -> button4 -> Text = "Earth Pony";

this -> button5 -> Text = "Unicorn";

this -> button2 -> Visible = true;

this -> button2 -> Text = "Pegasus";

YesorNo = 6;

}

else if (YesorNo == 6)

{

Race = "Unicorn";

this -> button2 -> Visible = false;

this -> button4 -> Visible = false;

this -> button5 -> Visible = false;

label2 -> Text = "Thank You For Playing!!";

YesorNo = 7;

this -> timer1 -> Start();

}

}

private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {

this -> button3 -> Text = "None yet";

}

private: System::Void textBox1_TextChanged_1(System::Object^ sender, System::EventArgs^ e) {

this -> label1 -> Visible = true;

CharName = textBox1 -> Text;

label1 -> Text = CharName;

this -> button5 -> Visible = true;

this -> button5 -> Text = "Done?";

YesorNo = 4;

}

private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {

TimerValue+= 1000;

if (TimerValue == 5000)

{

Form2 ^ f2 = gcnew Form2;

f2->Show();

this->Hide();

MoviePlay = 1;

StreamWriter^ outFile = gcnew StreamWriter("Movie.txt");

String^ Movie = MoviePlay.ToString();

outFile->Write(Movie);

outFile->Close();

this -> timer1 -> Stop();

}

}

};

}

Why can I not open Form 2?

2010\projects\retaliation\retaliation\Form1.h(431): error C2065: 'Form2' : undeclared identifier

Is refering to...

Form2 ^ f2 = gcnew Form2; at the bottom

Just in case it has to do with a problem in Form2...

 #pragma once

#include "stdAfx.h"

#include "Form1.h"

#include "Form2.h"

namespace Retaliation {

using namespace System;

using namespace System::ComponentModel;

using namespace System::Collections;

using namespace System::Windows::Forms;

using namespace System::Data;

using namespace System::Drawing;

using namespace std;

/// <summary>

/// Summary for Form2

/// </summary>

public ref class Form2 : public System::Windows::Forms::Form

{

public:

int MoviePlay;

Form2(void)

{

InitializeComponent();

//

//TODO: Add the constructor code here

//

}

protected:

/// <summary>

/// Clean up any resources being used.

/// </summary>

~Form2()

{

if (components)

{

delete components;

}

}

private: AxWMPLib::AxWindowsMediaPlayer^ MoviePlayer1;

protected:

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(Form2::typeid));

this->MoviePlayer1 = (gcnew AxWMPLib::AxWindowsMediaPlayer());

(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->MoviePlayer1))->BeginInit();

this->SuspendLayout();

//

// MoviePlayer1

//

this->MoviePlayer1->Enabled = true;

this->MoviePlayer1->Location = System::Drawing::Point(0, 0);

this->MoviePlayer1->Name = L"MoviePlayer1";

this->MoviePlayer1->OcxState = (cli::safe_cast<System::Windows::Forms::AxHost::State^ >(resources- >GetObject(L"MoviePlayer1.OcxState")));

this->MoviePlayer1->Size = System::Drawing::Size(860, 497);

this->MoviePlayer1->TabIndex = 0;

this->MoviePlayer1->Enter += gcnew System::EventHandler(this, &Form2::axWindowsMediaPlayer1_Enter);

//

// Form2

//

this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);

this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;

this->ClientSize = System::Drawing::Size(859, 495);

this->Controls->Add(this->MoviePlayer1);

this->Name = L"Form2";

this->Text = L"Form2";

this->Load += gcnew System::EventHandler(this, &Form2::Form2_Load);

(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->MoviePlayer1))->EndInit();

this->ResumeLayout(false);

}

#pragma endregion

private: System::Void axWindowsMediaPlayer1_Enter(System::Object^ sender, System::EventArgs^ e) {

}

private: System::Void Form2_Load(System::Object^ sender, System::EventArgs^ e) {

if (MoviePlay == 1)

{

MoviePlayer1->URL = "\\Scv183\\Scene1.mpeg";

MoviePlayer1 -> Ctlcontrols -> play();

}

}

};

}

网友答案:

You are getting an error because you have a circular dependency in your header files. Your compiler is trying to compile Form2.cpp, where you include Form1.h, in Form1.h you include Form2.h, and in Form2.h you include Form1.h. You need to use a forward declaration, or to change your design so that you class are less coupled.

Moreover don't include a headerfile in itself..

More generally, you should not implement your function definitions in header files, but use your .cpp files for that. I also don't think you need to use precompiled headers (stdAfx.h), you should disable the check-box when you create a Visual Studio project.

分享给朋友:
您可能感兴趣的文章:
随机阅读: