Jump to content
  • Advertisement
Sign in to follow this  
geo2004

Need help with vectors and class structure...

This topic is 4135 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

Ok, here is my dilemma. I am attempting, slowly, to create a very simple 2D game. I currently have several classes set up. My problem is with when to create "players", and how to store them. I had everything working fine with arrays and structures, but now I want classes, for inheritence and methods, etc, and I also want to use vectors. Here is an example of a couple classes: This is all from memory, so some things may be slightly off.
Class BaseObject{
  BaseObject();
Public:
    GLfloat x;
    GLfloat y;..etc etc...
}

Class Player : BaseObect{
   Player();
  Public:
    GLfloat dx;
    // A few more attributes here...
    void init(){
       player.x = float(rand().....);
       // more stuff here
    }
}

Hang with me here, its been over a year since I did much C++ class stuff in school. My question is, where should I declare my vector? I want a vector to hold the players, so do I put vector<Player> myVector as a private variable in Player? I did this, but when i was in my main code, i couldn't do a for loop with myVector.size(). Also, how exactly do i go about initiating a new player? I want to add a new Player when i hit a keyboard button. So in my code for the 'm' button, should I do Player player;player.init(), then do a push_back in the init function to add it to myVector? Or should I just do all of that initiation stuff in the constructor, and pass the needed stuff in as variables? I hope this isn't too confusing. It's monday morning and my brain isn't all the way on yet. Jeff Edit: How do I get my code in a box, with the scrollbars on the side? [Edited by - geo2004 on June 25, 2007 10:09:11 AM]

Share this post


Link to post
Share on other sites
Advertisement
Quote:

Hang with me here, its been over a year since I did much C++ class stuff in school.
My question is, where should I declare my vector? I want a vector to hold the players, so do I put vector<Player> myVector as a private variable in Player? I did this, but when i was in my main code, i couldn't do a for loop with myVector.size().


The vector containing a list of players or other game obejcts should not go in the player class, it should go outside the player class. Common practice is to have some kind of class that represents the current state of the game; that's where the list of active players would be.

Quote:

Also, how exactly do i go about initiating a new player? I want to add a new Player when i hit a keyboard button. So in my code for the 'm' button, should I do Player player;player.init(), then do a push_back in the init function to add it to myVector? Or should I just do all of that initiation stuff in the constructor, and pass the needed stuff in as variables?

It is preferable to perform your initialization in the constructor and avoid "init" functions; it's more idiomatic C++. The constructor is supposed to bring the object into a known, usable state. The presence of "init" functions implies this isn't the case, so they should, in general, be discouraged. You should become comfortable with them before you explore the few potential reasons to consider abandoning them.

Share this post


Link to post
Share on other sites
You don't have to store the vector in your player class you can store it in another class.

When m is hit you can create a player and push_back it to the vector.

vector<Player> myVector;
if(keyPressed('m')){
Player newPlayer;
myVector.push_back(newPlayer); //this will create a copy of newPlayer
// and add it to vector.
}

If you don't want to store actual player objects in the vector you can store pointers to them.Which has some benefits later on when using polymorphism etc.


vector<Player*> myVector;

if(keyPressed('m')){
myVector.push_back(new Player());
}

If you store pointers have a look at shared and smart pointers to avoid dangling pointer problems in the future.


Share this post


Link to post
Share on other sites
Ok, so putting your suggestions together, i should create another class, to hold information for the game...like so..?


Class cGame{
cGame();
Public:
vector<*Player> myVector;
vector<*Item> myItems;
// etc.....

}



And when I initiate a player, it should be done like...


// This...?
// 'm' key hit...
myVector.push_back(new Player(put in needed attributes here));



And then change the constructor to initialize the needed attributes?

Thanks for the replies so far guys!

Jeff

[Edited by - geo2004 on June 25, 2007 10:47:55 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by geo2004
Ok, so putting your suggestions together, i should create another class, to hold information for the game...like so..?

*** Source Snippet Removed ***


'class' and 'public' should be in lowercase, and *'s in type names go *after* the name of the thing being pointed to.

Quote:
And when I initiate a player


When you *instantiate* it, yes.

Share this post


Link to post
Share on other sites
OK, I took home these ideas last night and added the necesarry code. However, I can't access my new vectors at all. Heres how it looks:

BaseObject and Player class same as before, just added constructor for Player.
Player(GLfloat X,GLfloat Y, GLuint TEX){assign necesary stuff here}

Then I added a Game class. it looks like this.

class cGame
{
public:
cGame(void);
vector<Player*> player;
vector<Item> item;
public:
~cGame(void);
};




however, when I try to instantiate a new Player, I can't do this:
Player temp(1.0f, 1.0f,1);
player.push_back(temp);

When I type player., it won't recognize that as a vector and won't pop up all the methods.

I'm including Player.h in my cGame class, and including cGame.h and Player.h in my main .cpp, as well as <vector> in all the .h files and cpp's. Any one see why this isn't working?

Thanks for the help so far!

Jeff

Share this post


Link to post
Share on other sites
Quote:

however, when I try to instantiate a new Player, I can't do this:
Player temp(1.0f, 1.0f,1);
player.push_back(temp);

player is a vector of Player*, but temp is a Player. The types are not compatible. You can change player to a std::vector<Player> and that will work, or you can change temp to: Player *temp = new Player(1.0f,1.0f,1); and that will work.

I would tend to recommend the former option, since otherwise you either have to manually free all the memory you new (by iterating the players container in the destructor of cGame and calling delete on every element), or use smart pointers (such as Boost's shared_ptr). Both involve more work than just correcting the type of the players container, and I'm not sure there is a need for you to store pointers in the list yet.

Quote:

When I type player., it won't recognize that as a vector and won't pop up all the methods.

Intellisense is not perfect. Deleting the .ncb files in your project folder will force a reparse of the Intellisense database which sometimes helps fix the problem. Which IDE are you using? Hopefully not VC++6...

Share this post


Link to post
Share on other sites
I'm using Visual Studio 2005 Express, C++.

I changed <Player*> to <Player>, and deleted the .ncb.

when I do player., it still doesn't pop anything up. So I went ahead and did player.push_back(temp), compiled it, and it says that its missing a specifier on that line - int assumed. It also has an error in my cGame.cpp, it says std::vector - no appropriate default constructor...?

This is a mess.....

Jeff

Share this post


Link to post
Share on other sites
It sounds like you don't actually have #include <vector> where you need it. You may also have missing brackets someplace. Can you post your code (actual code) and the exact error message?

Share this post


Link to post
Share on other sites
cGame.h code:

#pragma once
#include <windows.h>
#include "stdafx.h"
#include <stdlib.h>
#include <vector>
#include "BaseObject.h"
#include "Player.h"


using namespace std;

class cGame
{
public:
cGame(void);
vector<Player> player;
vector<Item> item;
public:
~cGame(void);
};



and my Player.h code:

#pragma once
#include <glut.h>
#include <math.h>
#include <ilut.h>
#include <il.h>
#include <vector>
#include "BaseObject.h"

using namespace std;

class Player :public BaseObject
{
public:
Player(GLfloat X,GLfloat Y, GLuint TEX){
x=X;
y=Y;
dx=X;
dy=Y;
alive=true;
selected=false;
tex=TEX;
}
GLfloat dx;
GLfloat dy;
public:
~Player(void);
int getX()
{
return x;
}
};



Then in my main .cpp, i do this:
Player temp(1.0f,1.0f,2);
player.push_back(temp);

Errors:
c:\...\thegame\cgame.cpp(7) : error C2512: 'std::vector' : no appropriate default constructor available
c:\...\thegame.cpp(21) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!