Vector issues through multiple source files

Started by
2 comments, last by Tsuki no Hikari 19 years, 6 months ago
At the moment I'm trying to create a vector of character classes for my basic RPG. At the moment it compiles file until I try a player.push_back(), which gives me a "no matching function for call to `std::vector<Player, std::allocator<Player> >::push_back()'" error. Am I missing something?

// class.h
// This file will declare all classes and class prototypes

#ifndef CLASS_H
#define CLASS_H

#include "enum.h"

class Player
{
   public:
   Player();
   void playerCreate();

   characterClass cClass;
   elementType    eType;

   char  *name;
   long  hp;          // Must be negative to test if < 0
   long  hpmax;
   long  mp;          // Must be negative to test if < 0
   long  mpmax;
   long  pAttack;     // Physical Attack
   long  pDefense;    // Physical Defense
   int   pEvade;      // % Chance of evading (0-255%)
   int   pLuck;       // % Chance of critical hit (0-75%)
   long  pSpeed;
   long  mAttack;     // Magic Attack
   long  mDefense;    // Magic Defense
   int   mEvade;      // % Chance of evading magical attacks (0-255%)
   int   level;       // (1-1000)
   long  exp;         // (0-1,000,000,000)
   int   kills;       // Number of kills for this character
   long  ap;          // AP for weapon & magic skills

   int incHP;         // 
   int incMP;         // CONSTANT
   int incAttack;     // Values
   int incDefense;    // Used
   int incMagic;      // for
   int incMdef;       // levelUp()
   int incSpeed;      // 
};
#endif


// class.cpp
// This file will initialize all class information

#include <cstdio>
#include <cstdlib>
#include <vector>

#include "class.h"
#include "stardust.h"
#include "enum.h"



Player::Player()
{
   name     = "";
   hp       = 0;   hpmax    = 0;   mp       = 0;   mpmax    = 0;
   pAttack  = 0;   pDefense = 0;   pEvade   = 0;   pLuck    = 0;   
   pSpeed   = 0;   mAttack  = 0;   mDefense = 0;   mEvade   = 0;  
   level    = 0;   exp      = 0;   kills    = 0;   ap       = 0;
}



void Player::playerCreate(   )
{

   cClass      = charClassNone;
   eType       = eleNone;
   pAttack     = 0; 
   pDefense    = 0;
   mAttack     = 0;
   mDefense    = 0;
   pSpeed      = 0;
   hpmax = hp  = 0;
   mpmax = mp  = 0;
   incHP       = hpmax - 2;
   incMP       = mpmax - 2;
   incAttack   = pAttack - 2;
   incDefense  = pDefense - 2;
   incMagic    = mAttack - 2;
   incMdef     = mDefense - 2;
   incSpeed    = pSpeed - 2;
   level       = 1;

}


std::vector<Player> player(6);


// stardust.cpp
// This file will run the main module of the program

#include "class.h"
#include "stardust.h"
#include "enum.h"

#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <vector>


int main(int argc, char *argv[])
{
   player.push_back();
   srand( time(NULL) );
   printf("Holding...");
   getchar();


   return 0;
}

And I have another .h file that declares extern std::vector<Player> player; Can anyone help? I'm just getting into vectors and my books really don't go enough into it.
Advertisement
push_back() takes a parameter that specifies the object to add to the vector. You're not supplying one. Try changing to code to:
Player thePlayer;
player.push_back(thePlayer);
Your problem is this line right here (at the top of main in stardust.cpp):

player.push_back();

std::vector::push_back takes one argument, which is a const reference to whatever type it's holding. If you want to push back a blank player object, you should do something more like this:

player.push_back(Player());

Hope that helps.

-RC
Man I really feel like I'm in freaking idiot mode today. I'm off a lot.. I didn't even catch the fact that I was calling a function outside of any functions earlier... Man..bed, please give me back my brain..

This topic is closed to new replies.

Advertisement