G++ compiler being weird.

Started by
27 comments, last by Bregma 13 years, 3 months ago
I have a parent class called Entity and another class named CrawlerTurret which derives from the parent class Entity. When I go to compile and get the object file of the files, however, g++ informs me that there is a member function in there that is not of class Entity. Here's some code and what I'm typing in:

CrawlerTurret.h - FULL
#ifndef CRAWLERTURRET_H#define CRAWLERTURRET_H#include "Particle.h"#include "Map.h"#include "Entity.h"#include "Math.h"class CrawlerTurret : public Entity{public:	enum facing_enum {NONE, LEFT, RIGHT, UP, DOWN};		CrawlerTurret						();	CrawlerTurret						(int x, int y, int w, int h, INPUT_DEVICE = AUTONOMOUS);	static void push			(int x, int y, int w, int h, INPUT_DEVICE input = AUTONOMOUS);	void update					();	void render					(BITMAP* buffer, int mapxoff = 0, int mapyoff = 0);	void crawlerAI                   ();	private:	Entity* target;	facing_enum facing;	facing_enum lastContact;	int lastx, lasty;	int bufferedVelx, bufferedVely;};#endif


CrawlerTurret - SNIPPET
void CrawlerTurret::update(){		Entity::collision_correction(true); // <- This is causing problamos		Entity::update();	int px = 5.0f + fabs(vx);	int py = 5.0f + fabs(vy);	int d = 0; //shooting distance as not to collide with shooting entity	int pw = 5; //particle width	int ph = 5; //particle height	...


Entity.h - FULL
#ifndef _ENTITY_#define _ENTITY_#include <list>#include "Animation.h"#include <stdio.h>#include <math.h>#include "Math.h"using namespace std;//entity state will dictate parameters for how an entity should //react, animate and moveenum ENTITY_STATE{	IDLE = 0,	MOVING,	DEAD,};enum INPUT_DEVICE{	AUTONOMOUS = 0,	KEYBOARD,	JOY0,	JOY1,	JOY2,	JOY3};enum AFFILIATION{	NEUTRAL = 0,	FRIENDLY,	ENEMY};class Entity{public:	Entity					();	Entity					(int col_x, int col_y, int col_w, int col_h);	public://temporarily public for debug printing	static list<Entity*>	entity_list;	int x, y, w, h;			//entity source x,y,w,h		int bx, by, bw, bh;     //bitmap source x,y,w,h        float vx, vy;				//velocity x,y    float ax, ay;				//acceleration x,y	int life;				//if (life < 1) entity = dead; delete;	int damage;				//b.life -= a.damage when colliding	ENTITY_STATE state;	INPUT_DEVICE input;		//AI or keyboard/joystick controlled	AFFILIATION affiliation;	Animation idle;			//the run animation	Animation moving;		//other example animations	Animation dead;	bool inside					(int x,int y,int left,int top,int right,int bottom);		bool use_collision_correction;	bool colliding;public:	static void push					(int x, int y, int w, int h);	static void delete_dead_entities	();	virtual void update					();	virtual void render					(BITMAP* buffer, int mapxoff = 0, int mapyoff = 0);	void load_animation					(ENTITY_STATE animation, char* path, int frames);	Animation *get_animation			(ENTITY_STATE);		bool detect_collision				(Entity* b);	static Entity* get_entity			(int number);	static int get_count				();	void apply_force					(float x = 0, float y = 0);	void move_to						(int x = 0, int y = 0);	void move							();	void collision_correction           (bool on_off = true);	int get_x							();	int get_y							();	int get_w							();	int get_h							();	float get_vx							();	float get_vy							();	void set_life						(int life);	int get_life						();	int get_damage						();	int get_affiliation				();		//level collision routines	int collided_tl(int x, int y);	int get_user_data(int x, int y, int u);};#endif // _ENTITY_


Entity.cpp - SNIPPET
... void Entity::collision_correction(bool on_off){	use_collision_correction = on_off;}...


In Terminal:
g++ -c Entity.cpp Entity.hg++ -c CrawlerTurret.cpp CrawlerTurret.h-- Error in CrawlerTurret.cpp in update function: collision_correction is not a member of Entity



... But update and everything else in there is fine??
Holy crap, you can read!
Advertisement
It has been a while since I've written C++, so please forgive any errors. Some possible problem is that you are trying to call Entity::collision_correction() as a static method. I don't know if it is supposed to be an instance method. If so, you could use the 'this' pointer.
Right, but other things like Entity::update() work fine.

And I sent the exact code to another coder and it compiles fine with Dev-C++.
I'm trying to get experience by staying away from IDEs.

EDIT:
And this-> doesn't work.
Holy crap, you can read!
Last week, I decided I needed more experience driving my car.


So now I hop on one leg everywhere I go.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

So no real advice then?
Holy crap, you can read!
Well first check to make sure that your version of g++ is up to date. If that doesn't help then make a copy of your project and start deleting stuff until you can identify exactly whats making things go screwy or get down to a minimal compilable code sample that you can post here.
Try without the Entity:: part. Unless you have overrrided/hidden the function, there is usually no need to qualify a call to a base member function with the name of said base class.

Entity::update has no problems as CrawlerTurret hides the Entity::update function, so by calling Entity::update the compiler knows which version you are trying to call.

As was said above, looks like GCC is trying to find a static function called collision_correction. Weird bug, might want to report it as I'm not sure if it's proper C++ behaviour (assuming you can replicate it with test code).
Hrmm. Well, I removed the Entity:: part and got a new error.

CrawlerTurret.cpp:41: error: ‘collision_correction’ was not declared in this scope


So instead of it not being a member, it's not in the scope.
Holy crap, you can read!
Quote:Original post by Maverick Programmer
So no real advice then?

My advice is suspect dark preprocessor magic.

Try recompiling with the -save-temps command line switch and check the resulting .i file (the preprocessed file) to make sure everything is as you think it is.

Stephen M. Webb
Professional Free Software Developer

Perhaps unrelated, but you are not allowed to use the macro name _ENTITY_, because it's reserved to the implementation. Does anyone have a link to the relevant part of the standard handy?

This topic is closed to new replies.

Advertisement