What's wrong with "this" pointer? - c++

So, i decided to create a collision detection function in my game and i put it into the class of enemies. Then i put it to "enemy.cpp" and i used "this" pointer.
The code:
if(((this->sprite.getPosition().y + this->sprite.getTextureRect().height) >= blocklist[i].sprite.getPosition().y) &&
(this->sprite.getPosition().y <= blocklist[i].sprite.getPosition().y))
This gives me SIGSEGV segmentation error. I can't find what could be the problem in this line, here is the enemyclass for reference.
class enemyclass
{
public:
sf::Sprite sprite;
sf::Sprite bubble;
//PROPERTIES
float xspeed, yspeed;
int x, y;
float health;
bool colR, colL, colU, colD;
int skin;
void detectCollisions(int blocksize);
};
and the blockclass that i made a vector of to use as a list of enemies:
class blockclass
{
public:
sf::Sprite sprite;
int x, y;
};
I would be very thankfull for an answer since i can't find out what's wrong.

As others have said, if some complicated instruction causes an error, split it into smaller parts and see, in which part the error arises:
if( this->x == 0) // changes nothing
x = 0; // but validates 'this'
int thisy = sprite.getPosition().y;
int thish = sprite.getTextureRect().height;
sf::Sprite &that = blocklist[i].sprite;
if( that.x == 0) // validate 'that'
that.x = 0;
int thaty = that.getPosition().y;
if(thisy + thish >= thaty && thisy <= thaty) {
// ......
}

Related

C++ Object referencing in different scopes

I have a class Rect such that it holds the width, height, x and y values of a shape. The class can draw using the values in the parameter and move the drawn rect.
Rect::Rect(w, h, x, y, const std::string &image_path) : _w(w), _h(h),
_x(x), _y(y)
{
SDL_Surface *surface = IMG_Load(image_path.c_str());
if (!surface) {
std::cerr << "Failed to create surface.";
}
//create texture
texture = SDL_CreateTextureFromSurface(Window::renderer, surface);
if (!texture) {
std::cerr << "Failed to create worker texture.";
}
SDL_FreeSurface(surface);
}
Rect::~Rect()
{
SDL_DestroyTexture(texture);
}
Rect::draw()
{
//where the constructor parameters are parsed
SDL_Rect rect= {_x, _y, _w, _h} ;
//extra code about some SDL texture stuff and RenderCopy
}
Rect::moveX(int x){
_x +=x;
}
In my Unit class, I include the class Rect and I create my units, draw them in the same function. There is another function in unit that moves rect by checking another value from another class that changes.
Unit::Unit()
Unit::~Unit()
void Unit::createUnit(int type, int x, int y){
if (type == 0)
{
Rect unit1(unitImageWidth, unitImageSizeHeight, x, y, "res/unit1.png");
}
if (type == 1)
{
Rect unit2(unitImageWidth, unitImageSizeHeight, x, y, "res/unit2.png");
}
}
void Unit::moveUnit(int x){
if(selection == 0)
{
unit1.movex(x);
}
if (selection == 1)
{
unit2.movex(x);
}
}
My question is:
When in Unit::moveUnit(), how can I reference the object Rect "unit1" and Rect "unit2" that are initialized in Unit::createUnit()?
When I try to compile, it say that unit1 and unit2 are undefined.
You can't do what you want to do. Non-static local variables are bound to their scope. They are only visible inside and are destroyed when the program exits the area bounded by the {} braces.
The simplest solution is to go in a completely different direction. Add to Unit a private variable to contain the Rect for example,
Rect sprite;
Then replace
void createUnit(int type, int x, int y);
with a Unit constructor
Unit(int type, int x, int y);
And implement the constructor something like
Unit::Unit(int type, int x, int y): sprite(unitImageWidth,
unitImageSizeHeight,
x,
y,
type == 0? "res/unit1.png": "res/unit2.png")
{
}
The colon : starts a Member Initializer List and that crazy ?: Is a one line if statement called the Ternary or Conditional Operator
Note: I don't know what unitImageWidth and unitImageSizeHeight are or where they come from. Make sure you do and make sure they are accessible.
moveUnit becomes
void Unit::moveUnit(int x)
{
sprite.movex(x);
}
because sprite knows what it is and what image has been loaded and can move the Rect to x (or whatever movex does).
To use you
Unit myUnit(0, 1024, 42); // creates a unit of type 0 at coordinates 1024,42
myUnit.movex(88); // moves myUnit to 88,42 (I think)
Just add two Rect members in your class Unit, then you can use it in different member functions.
Better using pointer, as below:
class Unit
{
public:
Unit::Unit()
: uint1(NULL), uint2(NULL){};
Unit::~Unit()
{
if (uint1 != NULL) {
delete unit1;
uint1 = NULL;
}
if (uint2 != NULL) {
delete unit2;
uint2 = NULL;
}
};
void Unit::createUnit(int type, int x, int y)
{
if (type == 0) {
unit1 = new Rect(unitImageWidth, unitImageSizeHeight, x, y, "res/unit1.png");
}
if (type == 1) {
unit2 = new Rect(unitImageWidth, unitImageSizeHeight, x, y, "res/unit2.png");
}
}
void Unit::moveUnit(int x)
{
if (selection == 0) {
unit1->movex(x);
}
if (selection == 1) {
unit2->movex(x);
}
}
private:
Rect *unit1;
Rect *unit2;
};

Allegro swap object

I'm trying to swap to objects of class. It's a puzzle game something like http://www.neos-guide.org/sites/default/files/Fifteen_puzzle.png. I'm using std::swap but it doesn't work. Here's code:
if (puzzle[i-1][j].getNum() == 16) {
std::swap(puzzle[i][j], puzzle[i-1][j]);
}
else if (puzzle[i+1][j].getNum() == 16) {
std::swap(puzzle[i+1][j], puzzle[i][j]);
}
else if (puzzle[i][j+1].getNum() == 16 && j != 3) {
std::swap(puzzle[i][j+1], puzzle[i][j]);
}
else if (puzzle[i][j-1].getNum() == 16 && j != 0) {
std::swap(puzzle[i][j-1], puzzle[i][j]);
}
And here's my puzzle class:
class Puzzle {
private:
int x;
int y;
int num;
ALLEGRO_FONT *font;
public:
Puzzle();
~Puzzle();
void init(int posx, int posy, ALLEGRO_FONT *font);
void setNum(int num);
void draw();
void setXY(int x, int y);
int getX();
int getY();
int getNum();
bool click(int mx, int my);
};
I have tried std::swap on 2D array of integers, on two objects of same class, on 1D object array and 2D object array. It worked. It just doesn't work in this implementation. I also tried to comment out ALLEGRO_FONT as I thought that there is the problem. But nothing has changed. cplusplus.com says that I need to include <algorithm> or <utility>. I tried both (not at the same time) but it didn't work. Any suggestions? Thank you :)

C++ double pointer member access

C++ (Arduino wrapper) question: I'm writing a shoot em up game on an Arduino which has a LCD connected -
I have a base class (Sprite), and from this other classes are derived - Alien, Missile and Player. The constructor of the Alien class also has private member pMissile (a pointer to a Missile class) - 'an object within an object' would be a way to describe this I think.
[when an Alien fires a missile, it passes its own (x,y) coordinates to the missile, and the missile has its own method of moving starting from the Alien's coordinates]
My question is: How can I access the coordinates of the missile through the Alien object?
Streamlined code is below and I have also drawn a representation of the classes:
// Bass class - has a form/shape, x and y position
class Sprite
{
public:
Sprite(unsigned char * const spacePtrIn, unsigned int xInit, unsigned int yInit);
virtual void Move() = 0;
void Render() { display.drawBitmap(x,y, spacePtr, 5, 6, BLACK); }
unsigned int getX() const { return x; }
unsigned int getY() const { return y; }
protected:
unsigned char *spacePtr;
unsigned int x, y;
};
// Derived class "Missile", also a sprite and has a specific form/shape, and specific (x,y) derived from input sprite
class Missile : public Sprite
{
public:
Missile(): Sprite(&spaceMissile[0], 0, 0) {}
virtual void Move(); // its own method of moving
};
// Derived class "Alien" - has a specific form/shape, and specific (x,y) position
class Alien : public Sprite
{
public:
Alien();
virtual void Move(); // its own method of moving
private:
Missile *pMissile;
};
Alien::Alien(): Sprite(&spaceAlien[0], random(5, 75), random(4, 10))
{
Missile MissileArray[MaxAmmoSize];
pMissile = &MissileArray[0];
}
void Alien::Move()
{
if( random(10) % 2 == 0 )
x += 1;
if( random(10) % 3 == 0 )
y += 1;
if( (pMissile != NULL) && (random(10) == 1) )
{
pMissile->setCoord(x, y);
pMissile->Move(); // move the missile
pMissile++; // move onto the next missile in the array
}
Render();
}
/*****************************************************************************************/
Alien MONSTER;
Player HERO;
Alien *pMONSTER = &MONSTER;
void loop()
{
display.clearDisplay();
MONSTER.Move();
HERO.Move();
pMONSTER->getX(); // this is x location of MONSTER
**// how does pMONSTER access pMissile(x,y) within MONSTER.**
delay(100);
display.display();
}
Embedded C++ Class interaction
The common way is to add a getter function to Alien:
class Alien {
public:
Missile* getMissile() { return pMissile; }
}
To use it:
Alien* a = getAlienFromSomewhere();
auto pMissile = a.GetMissile();
if (pMissile != NULL) {
x = pMissile->getX();
y = pMissile->getY();
}
I imagine that you want to access your missile position through the alien to test the collision with your hero entity, but if you need to keep the track of your missiles you should not "walk" with your pointer to the next missile as shown in the Alien::Move(). Doing this you will lose the reference of the beginning of the array.
IMHO, I would do something like this in your alien class:
// Bass class - has a form/shape, x and y position
class Sprite
{
public:
Sprite(unsigned char * const spacePtrIn, unsigned int xInit, unsigned int yInit);
virtual void Move() = 0;
void Render() { display.drawBitmap(x,y, spacePtr, 5, 6, BLACK); }
unsigned int& getX() const { return x; }
unsigned int& getY() const { return y; }
protected:
unsigned char *spacePtr;
unsigned int x, y;
};
// Derived class "Missile", also a sprite and has a specific form/shape, and specific (x,y) derived from input sprite
class Missile : public Sprite
{
public:
Missile(): Sprite(&spaceMissile[0], 0, 0) {}
virtual void Move(); // its own method of moving
};
// Derived class "Alien" - has a specific form/shape, and specific (x,y) position
class Alien : public Sprite
{
public:
Alien();
~Alien(); // a destructor to cleanup your missiles - arduino have almost no memory to handle leaks ;-)
virtual void Move(); // its own method of moving
inline Missile& getMissile(unsigned char n) { return pMissile[n]; }
inline Missile& operator[](unsigned char n) { return getMissile(n); }
inline unsigned int& getX(unsigned char n) { return getMissile(n).getX(); }
inline unsigned int& getY(unsigned char n) { return getMissile(n).getY(); }
private:
Missile *pMissile;
// adding the code to handle the count
unsigned char missileCount;
};
Alien::Alien():
Sprite(&spaceAlien[0], random(5, 75), random(4, 10)),
missileCount(0)
{
// this way of allocation creates a local object that is destroyed by the end of this scope
//Missile MissileArray[MaxAmmoSize];
//pMissile = &MissileArray[0];
// so you should do somethin like this
pMissile = new Missile[MaxAmmoSize];
}
Alien()::~Alien()
{
delete[] pMissile;
}
void Alien::Move()
{
if( random(10) % 2 == 0 )
x += 1;
if( random(10) % 3 == 0 )
y += 1;
if( (pMissile != NULL) && (random(10) == 1) )
{
// my proposal to fire it up
Missile& missile = pMissile[missileCount];
missile->setCoord(x, y);
missile->Move(); // move the missile
missileCount++; // move onto the next missile in the array
}
Render();
}
Using the code like this you could access the locations of your missiles by using:
MONSTER.getX(0) += 1;
MONSTER[0].getY() +=1;
MONSTER.getMissile(1).getX() = 10;
To have some clarity I recommend also the refactoring of the getX() and getY() methods to x() and y(), since they are returning references to the class contents (and doing this, you should also rename your x and y members to something else, or you can get crazy with name conflicts).

class declaration error: insufficient contextual information to determine type

I'm a bit of a newcomer to programming and C++, and learning how to program games with Allegro 5. One of the projects I've set for myself is to clean up a tutorial source code of Pong that I found here: http://www.cppgameprogramming.com/newforums/viewtopic.php?f=5&t=1991
However, I've run into a problem. The compiler generates an error I don't entirely understand, and none of my research is panning out. The error is:
insufficient contextual information to determine type
The closest I found on the internet was this page: http://bytes.com/topic/c/answers/138897-error-insufficient-contextual-information-determine-type which helped me narrow down that the problem is in the class declarations. However, the solution provided there doesn't exactly apply to me, since the class constructors here take parameters. The question previously asked here doesn't seem to apply to my situation, either, since it used file output and templates, neither of which I'm using.
I've posted a large chunk of my program below, with the error-generating parts marked out with commented stars to make them easy to find, I hope. However, I've left in a lot of the rest of the code on the off-chance that it's somewhere else.
As a heads up: there might be bits of code you don't recognize from allegro 5, such as alObject.paint(255,255,255). That's me consolidating some of the allegro objects and functions into their own class to make it a bit easier on me, the source of which I'm not including here since the compiler doesn't generate errors with them.
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
#include "allegro.h"
struct CBox{
CBox(int _x, int _y, int _w, int _h):x(_x),y(_y),w(_w),h(_h){}
CBox(const CBox& other){
x = other.x;
y = other.y;
w = other.w;
h = other.h;
}
bool collides(const CBox& other){
return not(other.x + other.w < x or other.y + other.h < y or other.x > x + y or other.y > y + h);
}
int x;
int y;
int w;
int h;
};
class CPlayer{
private:
int score;
CBox box;
ALLEGRO_COLOR color;
double mov_y;
void testBounds(CBox&);
public:
CPlayer(CBox p, ALLEGRO_COLOR col):box(p),color(col){mov_y = 0.0;}
void setScore(int new_s){score=new_s;}
int getScore(){return score;}
void setBox(const CBox& b){box=b;}
CBox getBox(){return box;}
void setXYMovement(double new_my){mov_y=new_my;}
double getXYMovement(){return mov_y;}
void move(CBox& bounds);
void draw(){
al_draw_filled_rectangle(box.x, box.y, box.x + box.w, box.y + box.h, color);
}
};
class CBall{
private:
CBox box;
ALLEGRO_COLOR color;
double mov_y;
double mov_x;
int last_touch;
void testCollision(CBox&, const CBox&, CPlayer*);
int testBounds(CBox&, const CBox&);
public:
CBall(CBox p, ALLEGRO_COLOR col):box(p),color(col),last_touch(3){}
void setXYMovement(double new_mx, double new_my){
mov_x = new_mx;
mov_y = new_my;
}
void move(const CBox& bounds, CPlayer* plys);
void draw(){
al_draw_filled_circle(box.x + box.w/2, box.y + box.h/2, box.w/2, color);
}
};
class GameLoop{
private:
CBox fieldbox(int, int, int, int);
/************************************************/
/***********ERROR HERE?? ERROR HERE??************/
/************************************************/
CBall ball(const CBox&, ALLEGRO_COLOR);
CPlayer player1(const CBox&, ALLEGRO_COLOR);
CPlayer player2(const CBox&, ALLEGRO_COLOR);
/*************************************************/
/*************************************************/
public:
GameLoop(Allegro&);
void GameStart(Allegro&);
void runTimerChecks(ALLEGRO_EVENT&, Allegro&);
void runExit(ALLEGRO_EVENT&, Allegro&, bool&);
void playerInput(ALLEGRO_EVENT&, bool&);
void endPlayerInput(ALLEGRO_EVENT&);
};
void CPlayer::move(CBox& bounds){
//make sure the player doesn't go off-bounds
testBounds(bounds);
box.y+=(int)mov_y;
//Players can't move horizontally, so no bounds checking in that matter
}
void CPlayer::testBounds(CBox& bounds){
if((mov_y < 0) && (box.y + mov_y < bounds.y)){
box.y = bounds.y;
mov_y = 0;
}
else if((mov_y > 0) && (box.y + box.h > bounds.y + bounds.h)){
box.y = bounds.y + bounds.h - box.h;
mov_y = 0;
}
}
//ghostbox is the precalculated ball's trajectory
void CBall::move(const CBox& bounds, CPlayer* plys){
CBox ghostbox(box.x+(int)mov_y, box.y+(int)mov_y, box.w, box.h);
// test collision for box players
testCollision(ghostbox, bounds, plys);
testBounds(ghostbox, bounds);
}
void CBall::testCollision(CBox& ghostbox, const CBox& bounds, CPlayer* plys){
for(int i = 0; i < 2; i++){
//a player cannot touch the ball twice in a row
if(i != last_touch){
CBox other = plys[i].getBox();
if(ghostbox.collides(other)){
//set the last touch to this player
last_touch = i;
//negate the "ghost movement" in x axis
ghostbox.x -= (int)mov_x;
//bounce horizontally
mov_x = -mov_x;
//bounce vertically to change the ball's trajectory
mov_y = (((box.y+box.h/2.0)-(other.y+other.h/2.0))/other.h)*10;
break;
}
}
}
}
int CBall::testBounds(CBox& ghostbox, const CBox& bounds){
if(ghostbox.y < bounds.y){
ghostbox.y = bounds.y;
mov_y = -mov_y;
}
else if(ghostbox.y + ghostbox.h > bounds.y + bounds.h){
ghostbox.y = (bounds.y + bounds.h - ghostbox.h);
mov_y = -mov_y;
}
if(ghostbox.x + ghostbox.w < bounds.x){
box.x = bounds.x + bounds.w/2 - bounds.w/2;
box.y = bounds.y + bounds.h/2 - bounds.h/2;
return 2;
}
else if(ghostbox.x > bounds.x + bounds.w){
box.x = bounds.x + bounds.w/2 - box.w/2;
box.y = bounds.y + bounds.h/2 - box.h/2;
return 1;
}
box = ghostbox;
return 0;
}
GameLoop::GameLoop(Allegro& alObject){
// This box is our playfield (covers the whole screen)
fieldbox(0,0,alObject.getWidth(), alObject.getHeight());
//we setup the ball at the center of the screen with a white color
ball(CBox(alObject.getWidth()/2-10,alObject.getHeight()/2-10,20,20),alObject.paint(255,255,255));
//red player on the left
player1(CBox(10,alObject.getHeight()/2-80/2,20,80), alObject.paint(255,0,0));
//blue player on the right
player2(CBox(alObject.getWidth()-10-20,alObject.getHeight()/2-80/2,20,80), alObject.paint(0,0,255));
}
void GameLoop::GameStart(Allegro& alObject){
/*
when this variable is set to true the program will quit the main loop
and free the allocated resources before quitting.
*/
bool exit = false;
//we tell the ball to move to the left
/***********************************************/
/***********************************************/
ball.setXYMovement(-5.0,5.0); // GENERATES THE ERROR
/***********************************************/
/***********************************************/
while(!exit){
al_wait_for_event(alObject.eventq, NULL);
while(al_get_next_event(alObject.eventq, &alObject.event)){
if(alObject.event.type == ALLEGRO_EVENT_TIMER){
runTimerChecks(alObject.event, alObject);
}
else if(alObject.event.type == ALLEGRO_EVENT_DISPLAY_CLOSE){
// quit if the user tries to close the window
runExit(alObject.event, alObject, exit);
}
else if(alObject.event.type == ALLEGRO_EVENT_KEY_DOWN){
playerInput(alObject.event, exit);
}
else if(alObject.event.type == ALLEGRO_EVENT_KEY_UP){
endPlayerInput(alObject.event);
}
}
}
}
void GameLoop::runTimerChecks(ALLEGRO_EVENT& event, Allegro& alObject){
if(alObject.event.timer.source == alObject.getTimer()){
//fill the screen with black
al_clear_to_color(alObject.paint(0,0,0));
//move and draw our two players
/**************************************************/
/**************************************************/
player1.move(fieldbox); // GENERATES THE ERROR
player1.draw(); // GENERATES THE ERROR
player2.move(fieldbox); // GENERATES THE ERROR
player2.draw(); // GENERATES THE ERROR
/**************************************************/
/**************************************************/
}
}
void GameLoop::runExit(ALLEGRO_EVENT& event, Allegro& alObject, bool& exit){
if(event.display.source == alObject.getDisplay()){
exit = true;
}
}
void GameLoop::playerInput(ALLEGRO_EVENT& event, bool& exit){}
void GameLoop::endPlayerInput(ALLEGRO_EVENT& event){}
Yes, the error is here:
CBall ball(const CBox&, ALLEGRO_COLOR);
CPlayer player1(const CBox&, ALLEGRO_COLOR);
CPlayer player2(const CBox&, ALLEGRO_COLOR);
This doesn't declare member variables called ball, player1 and player2, as you think it does (according to code like: player1.draw();). Instead, what you've written is a declaration of member functions with these names, taking in argument the parameters you've specified. Instead, you should do:
CBall ball;
CPlayer player1;
CPlayer player2;
Then in the constructor of GameLoop, initialize them with whatever value you want, using initialization lists:
GameLoop::GameLoop(Allegro& alObject) :
ball(/* ... */),
player1(/* ... */),
player2(/* ... */)
{
// ....
}

Class inter-dependence problems

Hey so I have three 2 classes: Screen, and Sprite : that depend on each other so if i define one i can not implement the second class to be defined into it as it has not been definded yet! Is there any simple solution to this that does not require a lot of re-coding?
Heres each class definition (Map is also a class):
the Sprite Class: move() wont work without the Screen Class definition....
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////// Sprite Class ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Sprite
{
public:
///////////////////// Get and SET all the privates ///////
Sprite(){};
Sprite(string a_name, char a_symbol, float a_health){
_name = a_name;
_symbol = a_symbol;
_health = a_health;};
char get_symbol() {return _symbol;};
void set_symbol(char _sym) {_symbol = _sym;};
float get_health() {return _health;};
void set_health(float _numb) {_health = _numb;};
void add_health (float _numb) {_health += _numb;};
string get_name() {return _name;};
string set_name(string _aName) {_name = _aName;};
int* get_location(){return _location;};
void set_location(int X, int Y) {
_location[0] = X;
_location[1] = Y;};
//////////////////////////////// Move ////////////
// WONT WORK UNTIL UNLESS SCEEN CLASS IS DEFINED BEFORE IT
bool move(Screen screen,int X, int Y)
{
bool OK = true;
////////////////////// check whats already there /////
char newLoc = screen.get_contents(_location[1]+Y,_location[0]+X);
if (newLoc == '|' || '/' || '_' || '=' || 'X' || 'x' )
OK = false;
if (OK == true)
{
_location[0] += X;
_location[1] += Y;
return true;
}
else
return false;
};
private:
string _name;
char _symbol;
float _health;
int _location[2];
};
And the screen class: (the sprite overloaded insert function won't work without Sprite def.
///////////////////////// SCREEN CLASS ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
class Screen
{
private:
/////////////////////////////////////////// Screen Variables ///////////////
string _name;
vector <string> _contents;
public:
Screen(string name){_name = name;
_contents.resize(24);};
~Screen(){};
//////////////////////////////////////////// Get contents ///////////////////////////
string get_contents(int Y) {return _contents[Y];};
char get_contents(int X, int Y) {return _contents[Y][X];};
//////////////////////////////////////////// Display (1 FPS) ///////////////////////////
void Display(int numbRefreshes) //
{ //
for(int t = 0; numbRefreshes > t;) //
{ //
UL_2 = GetTickCount()+overSec-UL_1; // Get Time in Milliseconds since start //
// GATE TO GETTING INTO THE DISPLAY FUNCTION (every 1 sec)
if (UL_2 >= 1000) //
{ //
overSec += 1000 - UL_2; //
// Wait another second before update (1000 milliseconds) //
UL_1+=1000; //
UL_3+= 1; //
char UL_3_string[6]; //
itoa(UL_3, UL_3_string, 10); //
// Tell the for loop 1 render is complete (/////)
t+=1; // (///)
// Update the time counter (/)
int B = sizeof(UL_3_string); // |
for(int I = 0; I < sizeof(UL_3_string); I++)
Screen::Insert(UL_3_string[I], 38+I, 22);
/////////////////// Draw each line of the Screen
for (unsigned int I = 0; I < _contents.size(); I++)
{
cout << _contents[I];
}
//////////////// draw any empty lines NOT WORKING WTF???????
for(int emptyLines = 24-_contents.size(); emptyLines > 0; emptyLines--)
{
cout << endl;
}
}
}
};
/////////////////////////////////////////// Insert ////////////////////////
/////////////////// map
bool Insert(Map& _map)
{
for (unsigned int I = 0; I<_map.getContents().size();I++)
{
_contents[I] = _map.getContents()[I];
}
return true;
};
/////////////////// string
bool Insert(string _string, int Y)
{
_contents[Y] = _string;
return true;
};
///////////////////// char
bool Insert(char _char, int X, int Y)
{
_contents[Y][X] = _char;
return true;
};
//////////////////// sprite
bool Insert(Sprite& _sprite)
{
_contents[_sprite.get_location()[0]][_sprite.get_location()[1]] = _sprite.get_symbol();
};
};
Screen theScreen("theScreen");
Thanks for any help!! =)
I would redesign - I can't see why a sprite needs to know about a screen. And what is this:
if (newLoc == '|' || '/' || '_' || '=' || 'X' || 'x' )
supposed to be doing? That is not how you test things in C++.
Other problems - don't pass strings by value, pass them as const references, and don't begin names with underscores.
First, split your classes to .h files and .cpp files, then use forward declaration (google for it, I can't post link). There are a few other errors in your code, you might like Bjarne Stroustrup's book "Language c++"