C++ C和x2B的Lnk2020错误+;程序
所以我想做的是使用一系列指针来跟踪正在繁殖的机器人,让它们通过碰撞来战斗。这是通过在指针数组中使用方法来实现的,但在构建它时,我不断收到LNK2020错误 这是VBot.h文件C++ C和x2B的Lnk2020错误+;程序,c++,visual-studio-2010,C++,Visual Studio 2010,所以我想做的是使用一系列指针来跟踪正在繁殖的机器人,让它们通过碰撞来战斗。这是通过在指针数组中使用方法来实现的,但在构建它时,我不断收到LNK2020错误 这是VBot.h文件 class VBot { public: VBot( int startX, int startY, Panel ^ drawingPanel ) : x(startX), y(startY), panel(drawingPanel), energy(100), image(NULL) { }; virtua
class VBot
{
public:
VBot( int startX, int startY, Panel ^ drawingPanel ) :
x(startX), y(startY), panel(drawingPanel), energy(100), image(NULL) { };
virtual ~VBot() { };
virtual void Move() = 0;
virtual int EnergyToFightWith() = 0;
bool IsDead() const { return energy <= 0; }
virtual void Show();
bool CollidedWith ( VBot * b ) const;
void DoBattleWith ( VBot * b );
protected:
int x, y; // Current position of the VBot
gcroot<Drawing::Bitmap ^> image; // Image displayed for the VBot
gcroot<Panel ^> panel; // Panel on which to show the VBot.
int energy;
VBot();
};
class CNNBot : public VBot
{
public:
CNNBot( int startX, int startY, Panel ^ drawingPanel ):
VBot(startX, startY, drawingPanel)
{
image = gcnew Drawing::Bitmap("HappyBot.bmp");
}
~CNNBot(){};
void Move();
int EnergyToFightWith();
bool IsDead() { return (VBot::IsDead()); }
virtual void Show() { VBot::Show();}
bool CollidedWith ( VBot * b ) { return VBot::CollidedWith(b);}
void DoBattleWith ( VBot * b ){ VBot::DoBattleWith(b);}
private:
static const int MOVE_VAL = 55;
static const int RIGHT_BOUND = 490;
static const int DOWN = 40;
static const int MAXY = 379;
bool switcher;
};
类VBot
{
公众:
VBot(int startX、int startY、面板^drawingPanel):
x(startX)、y(startY)、panel(drawingPanel)、energy(100)、image(NULL){};
虚拟的~VBot(){};
虚空移动()=0;
虚拟int EnergyToFightWith()=0;
bool IsDead()常量{返回能量CreateGraphics();
g->DrawImageUnscaled(图像,x,y);
g->~Graphics();
}
布尔VBot::与(VBot*b)常量碰撞
{
如果(b==NULL)
返回false;
返回(x+图像->宽度)>=b->x
&&(b->x+b->图像->宽度)>=x
&&(y+图像->高度)>=b->y
&&(b->y+b->图像->高度)>=y;
}
void VBot::DoBattleWith(VBot*b)
{
int mine=EnergyToFightWith();
int yours=b->EnergyToFightWith();
如果(我的==你的)
{
能源=能源-矿山/2;
b->energy=b->energy-yours/2;
}
void CNNBot::Move()
{
如果(此->开关)
{
此->x+=移动值;
如果(此->x>=右边界)
{
这个->x=0;
此->y+=向下;
如果(此->y>最大值)
{
此->切换器=错误;
这->y=最大值;
}
}
}
其他的
{
此->x+=移动值;
如果(此->x>=右边界)
{
这个->x=0;
这->y-=向下;
如果(此->y<0)
{
此->切换器=真;
这->y=0;
}
}
}
面板->失效();
}
这是BotContainer.h
#include <vcclr.h>
#include "VBot.h"
#include "stdafx.h"
#ifndef BOTCONTAINER_H
#define BOTCONTAINER_H
using namespace std;
//------------------------------------------------------------------------------
// This class is an array class that will contain pointers to bots that are spawned
//
class BotContainer
{
private:
static const int MAXARRAY = 1000; //this is the max size of the array
VBot * list[MAXARRAY]; //This is the array of pointers
int count; // the variable that keeps track of the array
public:
//-------------------------------------------------------------------------------
//this the constructor that sets count to zero
//-------------------------------------------------------------------------------
BotContainer(){ count = 0;}
//--------------------------------------------------------------------------------
//This method will check for a collision between the all possible combinations of
//two bots it is const
//---------------------------------------------------------------------------------
void checkCollisions() const;
//---------------------------------------------------------------------------------
//This method will destroy all dead bots in the array
//---------------------------------------------------------------------------------
void destroy();
//----------------------------------------------------------------------------------
//This method will take given bot pointer and add it to the list.
//The Parameters are a pointer to a VBot called inBot.
//This inBot will be added to the end of the array which will then increment count
//Before adding it checks to make sure the array isn't full.
//----------------------------------------------------------------------------------
void add(VBot * inBot);
void Add(CNNBot * inBot);
//----------------------------------------------------------------------------------
//This method will move all the elements in the array.
//----------------------------------------------------------------------------------
void moveAll();
};
#endif
#包括
#包括“VBot.h”
#包括“stdafx.h”
#ifndef BOTU H
#定义两个容器
使用名称空间std;
//------------------------------------------------------------------------------
//这个类是一个数组类,它将包含指向生成的bot的指针
//
类BotContainer
{
私人:
static const int MAXARRAY=1000;//这是数组的最大大小
VBot*list[MAXARRAY];//这是指针数组
int count;//跟踪数组的变量
公众:
//-------------------------------------------------------------------------------
//这是将count设置为零的构造函数
//-------------------------------------------------------------------------------
BotContainer(){count=0;}
//--------------------------------------------------------------------------------
//此方法将检查所有可能的组合之间的冲突
//两个机器人,是康斯特
//---------------------------------------------------------------------------------
void checkCollisions()常量;
//---------------------------------------------------------------------------------
//此方法将销毁阵列中所有死亡的机器人
//---------------------------------------------------------------------------------
无效销毁();
//----------------------------------------------------------------------------------
//此方法将获取给定的bot指针并将其添加到列表中。
//这些参数是指向称为inBot的VBot的指针。
//这个内繁殖将被添加到数组的末尾,然后增加计数
//添加之前,请检查以确保阵列未满。
//----------------------------------------------------------------------------------
无效添加(VBot*内置);
无效添加(CNNBot*内置);
//----------------------------------------------------------------------------------
//此方法将移动数组中的所有元素。
//----------------------------------------------------------------------------------
void moveAll();
};
#恩迪夫
这是BotContainer.cpp文件
#include "stdafx.h"
#include <vcclr.h>
#include "BotContainer.h"
//---------------------------------------------------------------------------------
//This method will delete all the deat bots in the array from the highest indexed
//dead bot. If a bot is deleted then the method will shift all higher indexed elements
// to one index less than its previous index
//-------------------------------------------------------------------------------------
void BotContainer::destroy()
{
for( int i = count - 1; i <= 0; i--)
{
if ( list[i]->IsDead() == true )
{
int current = i; //index of the dead array
delete list[i];
for( int j = i + 1; j < count; j++)
{
list[current] = list[j];
current = j; // index of the next element above the one just shifted
}
count--;
}
}
}
//------------------------------------------------------------------------------------
//This method will go throught the array calling move() for each bot in the array
//-----------------------------------------------------------------------------------
void BotContainer::moveAll()
{
for( int i = 0; i < count; i++)
{
list[i]->Move();
}
}
//-----------------------------------------------------------------------------------
//This method checks for collisions between any 2 bots in the array making sure to not
//test (ivsi) or (ivsj) and also testing (jvsi).
// This method is const therfore it doesn't change the array just tests it.
//-----------------------------------------------------------------------------------
void BotContainer::checkCollisions() const
{
for( int i = 0; i < count; i++)
{
if (list[i]->IsDead() != true)
{
for( int j = i + 1; j < count; j++)
{
if(list[j]->IsDead() != true)
{
if ( list[i]->CollidedWith(list[j]))
{
list[i]->DoBattleWith(list[j]);
}
}
}
}
}
}
//--------------------------------------------------------------------------------
//This method will add a pointer of a bot to the array checking first to make sure
//the array isn't full
//--------------------------------------------------------------------------------
void BotContainer::add(VBot * inBot)
{
if(this->count <= MAXARRAY)
{
list[count] = inBot;
count++;
}
}
void BotContainer::Add(CNNBot * inBot)
{
if(this->count <= MAXARRAY)
{
list[count] = inBot;
count++;
}
}
#包括“stdafx.h”
#包括
#包括“BotContainer.h”
//---------------------------------------------------------------------------------
//此方法将从最高索引中删除数组中的所有deat bot
//死机器人。如果删除机器人,则该方法将移动所有索引较高的元素
//比上一个索引少一个索引
//-------------------------------------------------------------------------------------
void BotContainer::destroy()
{
对于(int i=count-1;i IsDead()==true)
{
int current=i;//死数组的索引
删除清单[i];
对于(int j=i+1;jMove();
}
}
//-----------------------------------------------------------------------------------
//此方法检查阵列中任意2个机器人之间的冲突,确保不发生冲突
//测试(ivsi)或(ivsj)以及测试(jvsi)。
//此方法是常量,因此它不会更改数组,只需测试它即可。
//-----------------------------------------------------------------------------------
void BotContainer::checkCollisions()常量
{
for(int i=0;iIsDead()!=true)
{
对于(int j=i+1;jIsDead()!=true)
{
如果(列表[i]->与(列表[j])发生冲突
{
列表[i]->DoBattleWith(列表[j]);
}
}
}
}
}
}
//--------------------------------------------------------------------------------
//此方法将向数组添加一个bot指针,首先检查以确保
//数组未满
//--------------------------------------------------------------------------------
void BotContainer::添加(VBot*内置)
{
如果(this->count count components=(gcnew System::ComponentModel::Container());
#include "stdafx.h"
#include <vcclr.h>
#include "BotContainer.h"
//---------------------------------------------------------------------------------
//This method will delete all the deat bots in the array from the highest indexed
//dead bot. If a bot is deleted then the method will shift all higher indexed elements
// to one index less than its previous index
//-------------------------------------------------------------------------------------
void BotContainer::destroy()
{
for( int i = count - 1; i <= 0; i--)
{
if ( list[i]->IsDead() == true )
{
int current = i; //index of the dead array
delete list[i];
for( int j = i + 1; j < count; j++)
{
list[current] = list[j];
current = j; // index of the next element above the one just shifted
}
count--;
}
}
}
//------------------------------------------------------------------------------------
//This method will go throught the array calling move() for each bot in the array
//-----------------------------------------------------------------------------------
void BotContainer::moveAll()
{
for( int i = 0; i < count; i++)
{
list[i]->Move();
}
}
//-----------------------------------------------------------------------------------
//This method checks for collisions between any 2 bots in the array making sure to not
//test (ivsi) or (ivsj) and also testing (jvsi).
// This method is const therfore it doesn't change the array just tests it.
//-----------------------------------------------------------------------------------
void BotContainer::checkCollisions() const
{
for( int i = 0; i < count; i++)
{
if (list[i]->IsDead() != true)
{
for( int j = i + 1; j < count; j++)
{
if(list[j]->IsDead() != true)
{
if ( list[i]->CollidedWith(list[j]))
{
list[i]->DoBattleWith(list[j]);
}
}
}
}
}
}
//--------------------------------------------------------------------------------
//This method will add a pointer of a bot to the array checking first to make sure
//the array isn't full
//--------------------------------------------------------------------------------
void BotContainer::add(VBot * inBot)
{
if(this->count <= MAXARRAY)
{
list[count] = inBot;
count++;
}
}
void BotContainer::Add(CNNBot * inBot)
{
if(this->count <= MAXARRAY)
{
list[count] = inBot;
count++;
}
}
#pragma once
#include "stdafx.h"
#include <vcclr.h>
#include "BotContainer.h"
namespace Prog3
{
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 Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
x = 0;
y = 0;
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Panel^ pnlGameZone;
private: System::Windows::Forms::ComboBox^ cmbSelect;
protected:
private: System::Windows::Forms::Button^ btnBotSpawn;
private: System::Windows::Forms::TrackBar^ trackBar1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Label^ label4;
private: System::ComponentModel::IContainer^ components;
private:
/// <summary>
/// Required designer variable.
/// </summary>
int x;
int y;
BotContainer VList();
private: System::Windows::Forms::Timer^ tmrspeed;
private: System::Windows::Forms::NumericUpDown^ updownX;
private: System::Windows::Forms::NumericUpDown^ updownY;
#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());
this->pnlGameZone = (gcnew System::Windows::Forms::Panel());
this->cmbSelect = (gcnew System::Windows::Forms::ComboBox());
this->btnBotSpawn = (gcnew System::Windows::Forms::Button());
this->trackBar1 = (gcnew System::Windows::Forms::TrackBar());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->label4 = (gcnew System::Windows::Forms::Label());
this->updownX = (gcnew System::Windows::Forms::NumericUpDown());
this->updownY = (gcnew System::Windows::Forms::NumericUpDown());
this->tmrspeed = (gcnew System::Windows::Forms::Timer(this-
>components));
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >
(this->trackBar1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >
(this->updownY))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >
(this->updownY))->BeginInit();
this->SuspendLayout();
//
// pnlGameZone
//
this->pnlGameZone->BackColor =
System::Drawing::SystemColors::Window;
this->pnlGameZone->Location = System::Drawing::Point(53, 102);
this->pnlGameZone->Name = L"pnlGameZone";
this->pnlGameZone->Size = System::Drawing::Size(495, 379);
this->pnlGameZone->TabIndex = 0;
//
// cmbSelect
//
this->cmbSelect->DropDownStyle =
System::Windows::Forms::ComboBoxStyle::DropDownList;
this->cmbSelect->FormattingEnabled = true;
this->cmbSelect->Items->AddRange(gcnew cli::array< System::Object^
>(3) {L"CNN Bot\t", L"MSNBC Bot", L"FOX NEWS Bot"});
this->cmbSelect->Location = System::Drawing::Point(53, 52);
this->cmbSelect->Name = L"cmbSelect";
this->cmbSelect->Size = System::Drawing::Size(121, 21);
this->cmbSelect->TabIndex = 1;
//
// btnBotSpawn
//
this->btnBotSpawn->Location = System::Drawing::Point(427, 48);
this->btnBotSpawn->Name = L"btnBotSpawn";
this->btnBotSpawn->Size = System::Drawing::Size(87, 23);
this->btnBotSpawn->TabIndex = 4;
this->btnBotSpawn->Text = L"Spawn a Bot";
this->btnBotSpawn->UseVisualStyleBackColor = true;
this->btnBotSpawn->Click += gcnew System::EventHandler(this,
&Form1::tmrspeed_Tick);
//
// trackBar1
//
this->trackBar1->Location = System::Drawing::Point(552, 48);
this->trackBar1->Maximum = 800;
this->trackBar1->Minimum = 50;
this->trackBar1->Name = L"trackBar1";
this->trackBar1->Size = System::Drawing::Size(134, 45);
this->trackBar1->TabIndex = 5;
this->trackBar1->TickFrequency = 100;
this->trackBar1->Value = 50;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(50, 34);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(65, 13);
this->label1->TabIndex = 6;
this->label1->Text = L"Select a Bot";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(200, 35);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(53, 13);
this->label2->TabIndex = 7;
this->label2->Text = L"Starting X";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(306, 35);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(53, 13);
this->label3->TabIndex = 8;
this->label3->Text = L"Starting Y";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(549, 32);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(103, 13);
this->label4->TabIndex = 9;
this->label4->Text = L"Speed of Movement";
//
// updownX
//
this->updownX->Location = System::Drawing::Point(203, 53);
this->updownX->Name = L"updownX";
this->updownX->Size = System::Drawing::Size(85, 20);
this->updownX->TabIndex = 12;
//
// updownY
//
this->updownY->Location = System::Drawing::Point(309, 52);
this->updownY->Name = L"updownY";
this->updownY->Size = System::Drawing::Size(80, 20);
this->updownY->TabIndex = 13;
//
// tmrspeed
//
this->tmrspeed->Tick += gcnew System::EventHandler(this,
&Form1::tmrspeed_Tick);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(740, 493);
this->Controls->Add(this->updownY);
this->Controls->Add(this->updownX);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->trackBar1);
this->Controls->Add(this->btnBotSpawn);
this->Controls->Add(this->cmbSelect);
this->Controls->Add(this->pnlGameZone);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >
(this->trackBar1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >
(this->updownX))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >e) e)
(this->updownY))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void btnBotSpawn_Click(System::Object^ sender, System::EventArgs^
e) {
if ( cmbSelect->SelectedIndex == 0)
{
x = Decimal::ToInt32(updownX->Value);
y = Decimal::ToInt32(updownY->Value);
CNNBot newBot(x,y,pnlGameZone);
CNNBot * temp = &newBot;
VList().Add(temp);
}
}
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
}
private: System::Void tmrspeed_Tick(System::Object^ sender, System::EventArgs^ e)
{
VList().moveAll();
VList().checkCollisions();
VList().destroy();
}
};
};
BotContainer VList();
BotContainer VList;
VList.Add(temp);
VList().Add(temp);
VList.moveAll();
VList.checkCollisions();
VList.destroy();
VList().moveAll();
VList().checkCollisions();
VList().destroy();