I have this code
/////////////////////////////////////Gnome.cpp file
#include "Living.h"
class Gnome:public Living{
private:
public:
Gnome();
void drawObjects();
};
Gnome::Gnome()
{
spriteImg = new Sprite("graphics/gnome.bmp"); //<-------------this is where it says there is the error
loaded = true;
}
///////////////////////////////////Living.h file
#include <iostream>
#include "Sprite.h"
using namespace std;
class Sprite;
class Living{
protected:
int x,y;
static Sprite *spriteImg; //= NULL;
bool loaded;
void reset();
public:
Living();
~Living();
int getX();
void setX(int _x);
int getY();
void setY(int _y);
void virtual drawObjects() =0;
};
// living.cpp
#include "Living.h"
Living::Living()
{
x=y=0;
reset();
}
Living::~Living()
{
delete spriteImg;
}
void Living::setX(int _x)
{
x=_x;
}
int Living::getX()
{
return x;
}
void Living::setY(int _y)
{
y=_y;
}
int Living::getY()
{
return y;
}
void Living::reset()
{
spriteImg = NULL;
loaded = false;
}
// sprite.h
#include <GL/glut.h>
#include <string>
using namespace std;
class ImageLoader;
class Sprite
{
public:
/**
* Enable 2D drawing mode to draw our sprites. This function MUST be called before
* any sprite is drawn on screen using the draw method.
*/
static void enable2D();
/**
* Disables the 2D drawing. This can be called before you are done drawing all 2D
* sprites and want to draw 3D now.
*/
static void disable2D();
Sprite(string filename);
virtual ~Sprite();
virtual void draw();
virtual void drawLiving(int dir, int flag); //flag=1 move
virtual void draw(int X, int Y);
virtual void rotate(GLint degrees);
// getter and setter methods
GLint getAngle() const;
void setAngle(GLint degrees);
void setX(GLdouble x);
void setY(GLdouble y);
GLint getHeight() const;
GLint getWidth() const;
/**
* Sets the pivot point in relation to the sprite itself, that is using the object
* coordiantes system. In this coordiantes system the bottom left point of the object
* is at (0, 0) and the top right is at (1, 1).
*
* E.g. to set the pivot to be in the middle of the sprite use (0.5, 0.5)
* Default values are (1, 1).
* #param pivotX Can be any value, but when x is in the range [0, 1] the pivot is inside the
* sprite where 0 is the left edge of the sprite and 1 is the right edge of the sprite.
* #param pivotY Can be any value, but when y is in the range [0, 1] the pivot is inside the
* sprite where 0 is the bottom edge of the sprite and 1 is the top edge of the sprite.
*/
void setPivot(GLfloat pivotX, GLfloat pivotY);
GLfloat getPivotX() const;
GLfloat getPivotY() const;
GLdouble getX() const;
GLdouble getY() const;
/**
* Sets the pivot to be at the point where object's pivot is set.
* #param obj The reference object to whose pivot we will set this pivot to be.
* Note: if the obj pivot changes or the obj moves after the setPivot call has
* been issued, the pivot of this object will not reflect this changes. You must
* call setPivot again with that object to update the pivot information.
*/
void setPivot(const Sprite &obj);
/**
* Sets the scale of the object. A scale of (1.0, 1.0) means the sprite
* maintains its original size. Values larger than 1 scale the sprite up
* while values less than 1 shrink it down.
*/
void setScale(GLfloat x, GLfloat y);
private:
ImageLoader *image;
GLuint textureID;
GLint angle;
GLdouble x;
GLdouble y;
GLfloat pivotX;
GLfloat pivotY;
GLfloat scaleX;
GLfloat scaleY;
GLint step;
//GLint dir;
//-----------------------------------------------------------------------------
// Initializes extensions, textures, render states, etc. before rendering
//-----------------------------------------------------------------------------
void initScene();
/**
* A helper function taken from http://www.opengl.org/resources/features/OGLextensions/
* to help determine if an OpenGL extension is supported on the target machine at run-time
* #param extension The extension name as a string.
* #return True if extension is supported and false if it is not.
*/
bool isExtensionSupported(const char *extension) const;
};
// if you need the implementation plz let me know
//and finally the engine class
#include <conio.h>
#include "Living.h"
#include "Gnome.h"
using namespace std;
class Engine{
private:
vector<char*> gamemap;
int score;
vector <Living*> gameobjs;
void FindRandomPos(int &x_pos,int &y_pos);
int mapMaxX;
int mapMaxY;
void collisionDetecion(int pX,int pY);
public:
Engine();
void init(const char* mapname="defaultmap.txt",int gnomes=1,int traals=1,int diamonds=10);
void printMap();
void printMap2();
void movePotter();
void calculateScroll();
int getScore();
int getMapMaxX ();
int getMapMaxY ();
};
//and the partial engine.cpp
void Engine::init(const char* mapname,int gnomes,int traals,int diamonds)
{
int i=0;
// int z=0;
mapMaxX=0;
mapMaxY=0;
int x_pos,y_pos;
int countobjs=0;
//gnomes
for(int i=0;i<gnomes;i++)
{
FindRandomPos(x_pos,y_pos);
gameobjs.push_back( new Gnome ); //<---------------here is where the error should begin i think
gameobjs[countobjs]->setX(x_pos);
gameobjs[countobjs]->setY(y_pos);
gamemap[gameobjs[countobjs]->getX()][gameobjs[countobjs]->getY()]='3';
countobjs++;
}
}
so when I try to build it, the linker shows this error: Undefined reference to Living::spriteImg
It is a Linker error, which tells you that the Linker cannot find definition for the static member Living::spriteImg, this is because you just declare it in your class but you never defined it.
You ned to define the static member in your Living.cpp:
Sprite* Living::spriteImg = NULL;
Remember that you need to define it in one and only one .cpp file.
Good Read:
What is the difference between a definition and a declaration?
Living::SpriteImg is a static data member. You have only declared it in the class Living.
However, static data members also needs to be defined separately, preferably in a .cpp file.
// .cpp file
Sprite* Living::spriteImg = 0;
Related
Basically for some reason new object is wrong type. All source code is on github https://github.com/teuro/sfml-radar. If it's help please fork at will.
I have following class:
#ifndef _VIEW_HPP
#define _VIEW_HPP
#include <iostream>
#include "sfml_drawsurface.hpp"
class View {
protected:
View(Drawsurface& d) : drawer(d) {
std::clog << "View::View()" << std::endl;
}
Drawsurface& drawer;
virtual void draw() = 0;
};
#endif
That is base class for all different kind of views. Now I have derived sub-class
#ifndef _GAME_VIEW_HPP
#define _GAME_VIEW_HPP
#include <vector>
#include <iostream>
#include <typeinfo>
#include "view.hpp"
#include "../models/game.hpp"
class Gameview : public View {
public:
Gameview(Drawsurface& d);
~Gameview();
void draw();
private:
Drawsurface& drawer;
};
#endif // _GAME_VIEW_HPP
Then abstract class Drawsurface
/**
* drawsurface base for all graphics pure abstract
* provide only interface quite high-level
* 2014/06/02
* Juha Teurokoski
**/
#ifndef _DRAWSURFACE_HPP
#define _DRAWSURFACE_HPP
#include <string>
#include "../models/point.hpp"
class Drawsurface {
public:
bool font_loaded;
virtual void rectangleColor(Point& a, Point& b, unsigned int color) = 0;
virtual void lineColor(Point& a, Point& b, unsigned int color) = 0;
virtual void circleColor(Point& a, unsigned int rad, unsigned int color) = 0;
virtual void trigonColor(Point& a, Point& b, Point& c, unsigned int color) = 0;
virtual void trigonColor(Point& a, unsigned int size, unsigned int color) = 0;
virtual void load_font(std::string font) = 0;
virtual void draw_picture(std::string tiedosto, Point& a, bool center = false) = 0;
virtual void draw_text(std::string text, Point& a, unsigned int color = 0) = 0;
virtual int get_fontsize() = 0;
virtual void flip() = 0;
virtual void clear_screen() = 0;
virtual ~Drawsurface() { }
};
#endif
Now if I create new instance of sfml_drawsurface which is sub-class of Drawsurface. For some reason new object is Drawsuface istead of sfml_drawsurface. Below is sfml_drawsurface class.
#ifndef SFML_DRAWSURFACE_HPP
#define SFML_DRAWSURFACE_HPP
/**
* sfml-drawsurface provides basic drawing, pictures and text
* require drawsurface
* 2014/06/02
* Juha Teurokoski
**/
#include "drawsurface.hpp"
#include <vector>
#include <stdexcept>
#include <iostream>
#include <SFML/Graphics.hpp>
class sfml_drawsurface : public Drawsurface {
public:
sfml_drawsurface(sf::RenderWindow& window);
~sfml_drawsurface();
void rectangleColor(Point& a, Point& b, unsigned int color);
void circleColor(Point& a, unsigned int rad, unsigned int color);
void lineColor(Point& a, Point& b, unsigned int color);
void trigonColor(Point& a, Point& b, Point& c, unsigned int color);
void trigonColor(Point& a, unsigned int _size, unsigned int color);
void draw_picture(std::string tiedosto, Point& a, bool center = false);
void draw_text(std::string text, Point& a, unsigned int color);
void load_font(std::string font);
void clear_screen();
int get_fontsize();
void flip();
protected:
private:
sf::RenderWindow& window;
sf::Font font;
sf::Color active;
sf::Color normal;
};
#endif // SFML_DRAWSURFACE_HPP
I create new object like this:
sfml_drawsurface drawer(window);
this->gameview = new Gameview(drawer);
std::clog << typeid(drawer).name() << std::endl;
And everything seems to be right, because std::clog outout is '16sfml_drawsurface'.
Next place is draw-method then happens something really weird.
Same print is now '11Drawsurface'.
Looks like Mike had the right idea. From your Program.cpp file you have in your constructor:
Program::Program() {
Game game;
...
this->gamecontroller = new Gamecontroller(game); //Probably also bad
sfml_drawsurface drawer(window);
this->gameview = new Gameview(drawer);
}
The problem is that drawer ceases to exist once the constructor is finished leaving you with a dangling reference and undefined behaviour. Looks like you may have the same problem with the game variable.
Solution is to not have them as local variables but as either class members (preferred) or dynamically allocated (it depends how long you need to have them around).
I'm having a problem with #including files in C++. When I try to compile the following code
#ifndef TILE_H_INCLUDED
#define TILE_H_INCLUDED
#include "location.h"
#include "Thing.h"
#include "Container.h"
#include <string>
using namespace std;
class Tile //VIRTUAL CLASS
{
protected:
Container onTile; //holds objects that are on the tile
Location * loc; //location
Tile* N; //links to Tiles of that direction (null if nothing)
Tile* E;
Tile* S;
Tile* W;
public:
//no constructor because superclass?
//loc can't move
//actions
bool placeOnTile(Thing * i){return onTile.addItem(i);}
//put a thing on the tile (on the floor)
Thing* takeFrmTile(int i){return onTile.movItem(i);}
//take a thing from the tile (from the floor)
Thing* access(int i) {return onTile.getItem(i);}
//gets an item, but doesn't move it (for like, flipping switches)
//direction setters/getters
void setLoc(Location* i){loc = i;}
void setN(Tile* i){N = i;}
void setE(Tile* i){E = i;}
void setS(Tile* i){S = i;}
void setW(Tile* i){W = i;}
Location* getLoc(){return loc;}
Tile* getN(){return N;}
Tile* getE(){return E;}
Tile* getS(){return S;}
Tile* getW(){return W;}
//displays
void dispOnTile(){onTile.allDisplay();}
void dispSingle(int i){onTile.singleDisplay(i);}
};
I get the error message that "Container" and "Thing" are not defined. Why is this? The #includes look to me like they are coded correctly, and they've worked in the past. I assumed it might be an issue of the #included files being not end-bracketed correctly or not using the same namespace, but they are ended correctly (with a }; ) and they are using the standard namespace. What's up? I can't find an answer and I know it's got to be something simple. For the record, the #included files are below:
#ifndef CONTAINER_H_INCLUDED
#define CONTAINER_H_INCLUDED
#include "Thing.h"
using namespace std;
class Container
{
private:
Thing ** contents; //array of pointers to Things
int numItems; //count item
int maxSize; //maxSize
public:
//constructor
Container(int i) {contents = new Thing*[i]; numItems = 0; maxSize=i;}
//sets num of items (for set-size bags)
Container() {contents = new Thing*[100]; numItems = 0; maxSize=100;}
//creates array of things
~Container() {delete contents;} //cleanup
//actions
bool addItem(Thing* th); //adds item to bag (really just rereferences the pointer)
bool rmvItem(int i); //removes item in array pos i
Thing* getItem(int i); //returns a pointer to item at array pos i
Thing* movItem(int i); //moves an item (deletes it and returns it)
//construction tools
void setMax(int i){delete contents; contents = new Thing*[i];}
//displays
void allDisplay(); //displays entire contents of container, numerated
void singleDisplay(int i); //displays content item i
};
#endif // CONTAINER_H_INCLUDED
#ifndef LOCATION_H_INCLUDED
#define LOCATION_H_INCLUDED
#include <string>
#include <sstream>
#include "Tile.h"
using namespace std;
class Location //stores xy coordinates of something
{
int x; //0 is NOT on map
int y;
Tile* ti; //Locations contain pointers to tiles
public:
//constructors (mainly for debug)
Location(){x=y=0;} //put object OUT OF MAP
Location(int ix, int iy){x=ix;y=iy;} //put object AT loc on map
//setters
void setX(int ix){x=ix;} //sets x
void setY(int iy){y=iy;} //sets y
void setT(Tile*i){ti=i;} //sets Tile
//getters
int getX() {return x;}
int getY() {return y;}
string getloc() //return location as a string, separated by a comma
{
ostringstream locxy; //create stringstream obj to handle input
locxy << getX() << "," << getY() << ". "; //put x, space, y into stringstream
string locret = locxy.str(); //convert stringstream to string
return locret; //return string
}
};
#endif // LOCATION_H_INCLUDED
#ifndef THING_H_INCLUDED
#define THING_H_INCLUDED
#include <string>
#include "location.h"
using namespace std;
class Thing //superclass that will be the base for objects
{
protected:
Location * loc; //location (in or out of map)
string name; //name
string desc; //description
bool deletable; //deletable (for undestructible items)
bool takeable; //if you can put it in your inv
bool hasInv; //returns true if the item has an inventory
public:
//constructor/destructor (debug only)
Thing() //sets initial values
{loc = new Location(0, 0);
name = "Uninitialized";
deletable = takeable = true;
}
Thing(int ix, int iy) //sets location
{loc = new Location(ix, iy);
name = "Uninitialized";
deletable = takeable = false;}
~Thing() {delete loc;} //deletes allocated data
//getters
Location* getLoc() {return loc;} //returns the location
string getDesc(){return desc;} //returns the description
bool getDel(){return deletable;} //returns deletable status
bool getTake(){return takeable;} //returns takeable status
string getName(){return name;} //returns name
string dispLoc(){return loc->getloc();} //displays location
//setters
void setName(string s){name = s;} //sets name
void setDel(bool b){deletable = b;} //sets deletability
void setDesc(string d) {desc = d;} //sets desc
void setLoc(Location* l) {loc = l;} //sets loc
void setTake(bool b){takeable = b;} //sets takeability
//accessors
};
#endif // THING_H_INCLUDED
I believe this is because you have recursive dependincies. That is, your classes all depend on each other, which means that at some point one of the classes will not be able to compile, because in order to get compiled it will need declaration of a class, but, it will not be able to find the declaration, because it is in the header file that is already up there in the stack of "#include" and thus because of guarding "#ifdef" becomes empty.
To give you the example.
To compile Tile, you need declaration of Location, so, naturally, you #include "location.h". But, to compile declaration of Location, you need declaration of Tile, so, you #include "Tile.h". But Tile.h has been #include-ed already, so there is no declaration for it!
The way to fight such circular dependencies is to use incomplete class declaration. For example, instead of including location.h into Tile.h, write
class Location;
class Tile
{
Location* loc;
}
This works as long as Location is only used to declare a pointer or reference and no members of Location class are accessed.
Then, in you 'Tile.c' file you can #include "location.h" and allow your inplementation of Tile methods access to Location members.
You have an include-loop
Tile.h includes
location.h
Thing.h
Container.h
Container.h includes Thing.h
Thing.h includes location.h
location.h includes Tile.h
Oops.
So let us say in the .cpp, the first to be included is Container.h. Then that includes Thing.h before it declares anything. Thing.h in turn includes location.h before it declares anything. Then that includes Tile.h before it declares anything. In Tile.h, all the include guards make the recursive includes no-ops.
And thus in Tile.h, Thing and Container are unknown.
Trying to have my "layer" class, put 3 objects "drawObject", into it's vector, and getting out of scope error.
Everywhere I see they just do "vector >Class< vectorName"; and it works!
layer class:
#include <vector>
#include "DrawObject.h"
using namespace std;
class layer
{
public:
layer();
virtual ~layer();
private:
vector<DrawObject> objects; <--------------- Error here! "'DrawObject' was not declared in this scope"
};
DrawObject class:
#include <SDL/SDL.h>
#include "AnimationSet.h"
class drawObject
{
public:
drawObject(char* name, char* surfaceFile, int xPos, int yPos, int drawLevel, bool willMoveVar, int animationNumber);
drawObject(char* name, char* surfaceFile, int xPos, int yPos, int drawLevel, bool willMoveVar);
virtual ~drawObject();
SDL_Surface* loadImage(char* surfaceFile);
SDL_Surface* getSurface();
void setSurface(SDL_Surface* surface);
char* getName();
void setName(char* name);
int getID();
void setID(int ID);
float getX();
void addX(float xAdd);
void setX(float xSet);
float getY();
void addY(float yAdd);
void setY(float ySet);
bool isSprite();
void setIsSprite(bool isSprite);
bool willMove();
void setWillMove(bool willMove);
bool draw();
void setDraw(bool draw);
private:
SDL_Surface* surface = NULL; // Imagem, no caso de único sprite; caso contrário, spritesheet.
char* name;
int ID;
float xPos;
float yPos;
bool isSpriteVar; // Se isSprite, imagem é spritesheet.
bool willMoveVar;
bool drawVar;
};
vector<DrawObject> should be vector<drawObject>, no? C++ is case-sensitive, you defined the class drawObject but attempt to use DrawObject.
Hey I'm trying to pass a class object that I have made into another class to read that data. The error I'm getting is c2061: syntax error: identifier 'Player'
This is my Player2.h
#pragma once
#include "DarkGDK.h"
#include "Input.h"
#include "Player.h"
class Player2{
public:
Player2();
void PlayerSetup();
void PlayerUpdate(Player& user1);
void PlayerHealthReset();
void Gravity();
float GetPosX();
bool CheckMatchEnd();
void PlayerFire(Player& user1);
void PlayerCheckHitEnemies(Player& user1);
private:
float Vx;
float Vy;
float PosX;
float PosY;
float Speed;
int Lives;
int Health;
//
int gravity;
bool playerJumping;
bool matchEnd;
bool playerIsFiring;
float playerBullet;
bool directionBullet;
};
And the error I'm getting is that It can't recognize Player even though I brought in the Player header.
Here is Player.h
class Player{
public:
Player();
void PlayerSetup();
void PlayerUpdate(float PosX2);
void PlayerHealthReset();
float GetPosX();
float GetPosY();
void Gravity();
bool CheckMatchEnd();
void PlayerFire(float PosX2);
private:
float Vx;
float Vy;
float PosX;
float PosY;
float Speed;
int Lives;
int Health;
float playerBullet;
bool playerIsFiring;
int gravity;
bool playerJumping;
bool matchEnd;
bool directionBullet;
};
All the respective code within the header file works 100%, as I've tested it.
player does not compile before player2 is defined, so placing class player above your player2's declaration will compile player BEFORE moving onto player 2.
class player;
class player2{
//...
};
-Also as Hunter McMillen suggested think about making player 2 inherit from a base class, maybe player that defines standard methods all players would use(I dont want to steal hunter's idea, i'll let him post answer about this if he pleases with a more in depth approach).
So I've got two classes: Object and Player.
I want Player to inherit from Object because Object has basic functions that I need.
Player has it's added-on functions that expand from what Object can do.
I have four files:
Object.cpp, Object.h, Player.cpp, and Player.h.
To make an example out of my situation, I have added a variable to my Player class:
playerVariable. My Object constructor parameters does not contain this, however my Player constructor does, so you can see that Player expands Object.
Anyways, Here is my code:
Object.h:
#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>
class Object{
int x, y;
HTEXTURE tex;
hgeAnimation *anim;
float angle, FPS, velocity;
public:
Object(int x, int y, HTEXTURE tex, float FPS);
//Setters
void SetX(int x);
void SetY(int y);
void SetSpeed(int FPS); //Image Speed
void SetAngle(float angle); //Image Angle
void SetVelocity(float velocity); //Object Speed/Velocity
//Getters
};
Object.cpp:
/*
** Object
**
*/
#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>
#include "Object.h"
Object::Object(int x, int y, HTEXTURE tex, float FPS){
this->x = x;
this->y = y;
this->tex = tex;
this->FPS = FPS;
}
//Setters
void Object::SetX(int x){
this->x = x;
}
void Object::SetY(int y){
this->x = x;
}
void Object::SetSpeed(int FPS){
this->FPS = FPS;
anim->SetSpeed(FPS);
}
void Object::SetAngle(float angle){
this->angle = angle;
}
void Object::SetVelocity(float velocity){
this->velocity = velocity;
}
//Getters
Player.h:
#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>
#include "Object.h"
class Player : public Object{
int playerVariable;
public:
Player(int x, int y, HTEXTURE tex, float FPS, int playerVariable);
//Setters
void SetX(int x);
void SetY(int y);
void SetSpeed(int FPS); //Image Speed
void SetAngle(float angle); //Image Angle
void SetVelocity(float velocity); //Object Speed/Velocity
//Getters
};
Player.cpp:
/*
** Object
**
*/
#include <hge.h>
#include <hgesprite.h>
#include <hgeanim.h>
#include <math.h>
#include "Object.h"
#include "Player.h"
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable){
this->x = x;
this->y = y;
this->tex = tex;
this->FPS = FPS;
}
//Setters
void Object::SetX(int x){
this->x = x;
}
void Object::SetY(int y){
this->x = x;
}
void Object::SetSpeed(int FPS){
this->FPS = FPS;
anim->SetSpeed(FPS);
}
void Object::SetAngle(float angle){
this->angle = angle;
}
void Object::SetVelocity(float velocity){
this->velocity = velocity;
}
//Getters
My problem is, I'm not exactly sure if I'm setting this up right.
I've looked at tutorials on inheritance but I have no idea how to set it up with both
a .h file and a .cpp file for the classes. Can someone help?
You are defining the Object functionality twice. You don't need to define that, it will be inherited from Object and automatically be available on Player.
Your Player constructor needs to invoke the base Object constructor. This is done with constructor initialization lists:
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable)
: Object(x, y, tex, FPS) // forward arguments to base constructor
{}
You can call the base class constructor this way, specifying every parameter :
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable): Object(x, y, tex, FPS) {
It looks like you don't actually need class Player to override SetX(int), SetY(int), SetSpeed(int), SetAngle(float), and SetVelocity(float) of Object. If you did, then you would make these Object member functions virtual.
A few other notes:
You don't need to redefine the Object member functions in Player.cpp. Player is an Object, so it inherits the member functions of class Object. Also, if there are any differences between the implementation in Object.cpp and Player.cpp then you will get linker errors due to a violation of the One Definition Rule.
The Object destructor should be declared virtual.
Rather than setting the members of Object directly in the Player constructor, you could use an initialization list:
Player::Player(int x, int y, HTEXTURE tex, float FPS, playerVariable)
: Object(x, y, tex, FPS), playerVariable(playerVariable)
{
}
Using initialization lists is much more efficient. It is even necessary in this case because Object::x, Object::y, Object::tex, and Object::FPS are all private to Object and friends.
In Object, you should provide a custom copy constructor and copy-assign operator overload (Object::operator=(const Object&)). The C++ compiler provides default implementations of these member functions for you, but because you have pointer members and HTEXTURE members, you likely need to provide explicit implementations to handle the deep copy. The copy constructor and copy-assign overload must always make deep copies.
There is a mismatch on the type of FPS. The Object::FPS member is declared as a float, but the parameter to Object::SetSpeed(int) is an int.