0

所以我想做的是使用一组指针来跟踪产卵机器人并让它们通过碰撞来进行战斗。这是通过使用指针数组中的方法来完成的,但是在构建它时我不断收到 LNK2020 错误

这是 VBot.h 文件

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.cpp 文件

#include "stdafx.h"     

#include "Vbot.h"

void VBot::Show()
{ 
  Graphics ^ g = panel->CreateGraphics();
  g->DrawImageUnscaled( image, x, y );
  g->~Graphics();
 }


bool VBot::CollidedWith ( VBot * b ) const
{
 if (  b == NULL )
  return false;

return   ( x + image->Width ) >= b->x
  && ( b->x + b->image->Width ) >= x
  && ( y + image->Height ) >= b->y
  && ( b->y + b->image->Height ) >= y;

}


void VBot::DoBattleWith ( VBot * b )
{
 int mine = EnergyToFightWith();
 int yours = b->EnergyToFightWith();
if( mine == yours )
{
 energy = energy - mine / 2;
 b->energy = b->energy - yours / 2;
}
void CNNBot::Move()
{
if (this->switcher)
{
  this->x += MOVE_VAL;
  if( this->x >= RIGHT_BOUND)
{
    this->x = 0;
    this->y += DOWN;
    if(this->y > MAXY)
    {
        this->switcher = false;
        this->y = MAXY;
    }
 }
}
else
{
  this->x += MOVE_VAL;
  if( this->x >= RIGHT_BOUND)
{
    this->x = 0;
    this->y -= DOWN;
    if(this->y < 0)
    {
        this->switcher = true;
        this->y = 0;
    }
 }
}
panel->Invalidate();
}

这是 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

这是 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++;
}
}

这是 Form.h 文件

#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();
     }
};
};

这是我收到的错误消息。错误 1 ​​错误 LNK2020:未解析的令牌 (06000003) Prog3.Form1::VList C:\Users\Duerst\Documents\Visual Studio 2010\Projects\Prog 3\Prog 3\Prog 3.obj Prog 3

我是一个新手程序员,所以它可能是一些非常基本的东西,但如果你们能找到我做错了什么,那就太好了。

4

1 回答 1

1

问题是当你像这样声明 VList 时:

BotContainer  VList();

由于 () 运算符,编译器认为它是一个函数而不是一个对象。要修复它,请删除括号 ()

BotContainer  VList;

并且还删除了 VList 非常出现的括号:

VList.Add(temp);

代替:

VList().Add(temp);

和:

VList.moveAll();
VList.checkCollisions();
VList.destroy();

代替:

VList().moveAll();
VList().checkCollisions();
VList().destroy();
于 2012-10-25T01:20:52.680 回答