class vectors

Started by
1 comment, last by Triston 21 years, 7 months ago
I'm getting compiling errors on this code and I'm not sure why... What I am trying to do is create a vector of number of players (an array of class objects that make up the number of players) Then when the user inputs the number of players I can resize the vector to the correct size and then have an element in the vector for each player to hold his/her info...(name, cash on hand, etc...) in my main.cpp
      
#define WIN32_LEAN_AND_MEAN

#include <string>

#include <vector>

#include <windows.h>

#include "resource.h"

#include "main.h"

#include "General.h"

#include "Player.h"


std::string str;
using namespace std;
const char g_szClassName[] = "myWindowClass";
WNDPROC lpfnOldWndProc;

GENERAL General;
vector<PLAYER> vPlayers;
vPlayers.reserve(6);
//more code....  

  
player.cpp
            
#include "Player.h"

PLAYER::PLAYER()
{
	
	name.reserve(15);
	cash = 500;
}
PLAYER::~PLAYER()

{
}
    
player.h
            
#ifndef PLAYER_H
#define PLAYER_H
#include <vector>
using namespace std;

class PLAYER
{
	public:
		PLAYER();
		int cash;
		vector<char> name;
		~PLAYER();
	private:
};
#endif
      
when I compile this code I get the following errors in the main.cpp on this line "vPlayers.reserve(6);" d:\GDev\Cards\Ride2\main.cpp(17) : error C2143: syntax error : missing ';' before '.' d:\GDev\Cards\Ride2\main.cpp(17) : error C2501: 'vPlayers' : missing storage-class or type specifiers d:\GDev\Cards\Ride2\main.cpp(17) : error C2371: 'vPlayers' : redefinition; different basic types Any reasons why? I'm sure it is a minor problem that I'm overlooking... TIA Triston All the world's a stage...and I seem to fall off quite a bit. [edited by - Triston on September 2, 2002 1:43:24 PM] [edited by - Triston on September 2, 2002 1:44:29 PM]
All the world's a stage...and I seem to fall off quite a bit.
Advertisement
You can't call functions like that (vPlayers.reserve(6)) outside of a function. You can either move the reserve call into a function (like in you actual main () function), or if the default PLAYER is all you want initially, you can call this form of vector::vector:

vector<PLAYERS> vPlayers (6); // initializes w/ 6 "blank" PLAYERS

Those 6 players will be constructed by the default PLAYER ctor.


[edited by - Stoffel on September 2, 2002 1:55:25 PM]
Woo Hoo thanks...Works great..
I got all caught up in working with vectors I didn''t realize I was outside of a function...thanks

Triston

All the world''s a stage...and I seem to fall off quite a bit.
All the world's a stage...and I seem to fall off quite a bit.

This topic is closed to new replies.

Advertisement