This topic is now archived and is closed to further replies.

class vectors

This topic is 5579 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

#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;
//more code....  

#include "Player.h"

	cash = 500;

#ifndef PLAYER_H
#define PLAYER_H
#include <vector>
using namespace std;

class PLAYER
		int cash;
		vector<char> name;
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]

Share this post

Link to post
Share on other sites
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]

Share this post

Link to post
Share on other sites