class E
{
public:
// //Test
//static void run();
static int test;
};
//int E::test = -12;
/*
void E::run()
{
test = 99999;
}
*/
void main(){
E::test = 10;
//E::run();
}
static problem in c++
Hello, I'm doing a little space shooter game and because many of my objects of the same type will share some data I wanted to make it static. This is for example the vector list and class specifik images. However I fail to get it to work properly, here is a simple example that might show what I'm doing wrong.
Why does this issue a linker error?
main.obj : error LNK2001: unresolved external symbol "public: static int E::test" (?test@E@@2HA)
I tried the same thing but split over three files, 1: main.cpp 2: E.h 3: E.cpp, and then this works until I uncomment the static function run(). In which some other linker error occurs.
If I'm doing something strange, is there any other way to do it?
How is the correct syntax and use for static, and is there any special rules when it later comes to objects and not ints. Is there any special rules that involves static functions?
Thanks for any help
You'll need to uncomment this line
//int E::test = -12;
And make sure that it is in E.cpp and not E.h
//int E::test = -12;
And make sure that it is in E.cpp and not E.h
Yes, for ints, but somehow, doing the same with classes does not work. Perhaps you could show me how to properly add a static vector and a static function?
Could you please paste some code where you are attempting to do the same with classes.
That works fine.
Dave
#include <vector>class E{public: static std::vector<int> testVector;};std::vector<int> E::testVector;int main( int argc, char* argv[] ){ return 0;}
That works fine.
Dave
It should work the same way with objects.
E.g. for an object of a 3D vector, in Vector.h
and in Vector.cpp
(Well, don't debate over the sense of unzero(); its just an example. ;)
E.g. for an object of a 3D vector, in Vector.h
class Vector {public: float x,y,z; Vector(float a,float b,float c); static Vector ZERO; static void unzero();};
and in Vector.cpp
Vector::Vector(float a,float b,float c): x(a), y(b), z(c) {}Vector Vector::ZERO(0,0,0);void Vector::unzero() { ZERO.x = ZERO.y = ZERO.z = 1;}
(Well, don't debate over the sense of unzero(); its just an example. ;)
Quote:Original post by TrillobitThe syntax posted by Colin Jeanne above applies to any variable type, including std::vector. If it's not working for you, post the errors you're getting and I'm sure it'll be easy to spot the problem.
Yes, for ints, but somehow, doing the same with classes does not work. Perhaps you could show me how to properly add a static vector and a static function?
You mentioned you got different link errors when the static function definition was in the .cpp file; perhaps you could post those as well. Also, you should probably post the exact code that results in the errors, including all #include directives, and with nothing commented out.
Thanks for all replies.
here is my code:
----Main file----
---- Entity Header file ----
---- Entity cpp file ----
I get this error: ..\Entity.h(21) : error C2146: syntax error : missing ';' before identifier 'mPosition'
here is my code:
----Main file----
#include "vgc.h" // VGC, a library to draw simple graphics.#include <string>#include <sstream>//#include <iostream> // Not in use any more#include "Entity.h"class Engine{protected: // M E M B E R V A R I A B L E S // ----------------------------------------------- int width; int height;};//Classclass Zeroly : public Engine{ // C O N S T R U C T O R // ----------------------------------------------- public: Zeroly::Zeroly() { initiate(); while( VGC::beginLoop(), mainloop() ) { VGC::endLoop(); } VGC::endLoop(); } // M E M B E R F U N C T I O N S // ----------------------------------------------- // Called by Contructor private: void initiate() { width = 800; height = 600; VGC::initialize( "Zeroly", width, height ); } // This is the inside of the game loop bool mainloop() { if ( keyDown( VGCKey::ESCAPE_KEY ) ){ return false; }// False = Exit VGCDisplay::beginFrame(); Update(); //Clears the display and sets BackGround color -> Alpha, R, G, B VGCDisplay::clear( VGCColor(255,125,125,125) ); //clear screen VGCDisplay::endFrame(); return true; }//function main loop void Update() { //E::run();// = 200; //Entity::updateAll(); } // Called by Destructor void terminate() { VGC::finalize(); }// Function Terminate private: // Interface to VGC bool keyHit( VGCKey key ) { return VGCKeyboard::wasPressed( key ); } bool keyDown( VGCKey key ){ return VGCKeyboard::isPressed( key ); } // D E S T R U C T O R // ----------------------------------------------- public: ~Zeroly() { terminate(); }};//Classint VGCMain( const VGCStringVector &arguments ){ Zeroly Z; // Run Game return 0;}//main
---- Entity Header file ----
#pragma once#include <vector>#include "vgc.h"class Entity{public: Entity(void); ~Entity(void); int getCount(); // Updates all entities static void updateAll();public://protected: void update(void);public: VGCVector mPosition; VGCVector mVelocity; VGCVector mFrame; // Which image to draw in an animation int mHealth, mSize, mCollisionDamage; // Definition of STATIC member variables //----------------------------------------------- typedef std::vector<Entity*> Entity_Vector; typedef Entity_Vector::iterator Entity_Iterator; //Used to loop the entities // THESE STATICs does noot work because they are not initiated! static Entity_Vector collection;// Lists all entities static VGCImage mImage;};
---- Entity cpp file ----
[source = lang("cpp")]#include "Entity.h"#include "vgc.h"Entity_Vector Entity::collection;// Lists all entitiesEntity::Entity(void){ }Entity::~Entity(void){}int Entity::getCount(){ return Entity::collection.size();}void Entity::updateAll()// S T A T I C//------------------------------------------------------------// Updates all entities - is staticvoid Entity::updateAll(){ // Loop all created entity objects - Player,Enemy,Shot //Used to loop the entities // NOTE: PASTED HERE IN DEBUGGIN PURPOSE - also in .h file// typedef std::vector<Entity*> Entity_Vector;// typedef Entity_Vector::iterator Entity_Iterator; for( Entity_Iterator p = collection.begin(); p != collection.end(); p++ ) { //(*p).update(); // see member function below // ERROR!! p->update() if ( p->mHealth <= 0 ) { collection.erase( p ); // collection is a static member of this class delete (p); } } return true;}//------------------------------------------------------------void Entity::update(void){ collision(); mPosition += mVelocity; VGCDisplay::renderImage(mImage, mFrame, mPosition, VGCAdjustment( 0.5, 0.5 );}void Entity::collision(void){ for( Entity_Iterator p = collection.begin(); p!= collection.end(); p++) { if ( &this != p ) { int deltaX = this.mPosition- p->mPosition; int deltaY = this.mPosition- p->mPosition; if(((deltaX*deltaX) + (deltaY*deltaY)) <= ((this.mSize*this.mSize) + (2*this.mSize*(p->mSize)) + p->mSize * p->mSize ) ) { mHealth -= p->mCollisionDamage; } } } // next}
I get this error: ..\Entity.h(21) : error C2146: syntax error : missing ';' before identifier 'mPosition'
For starters, it doesn't look like you've included the header file for VGCVector in the Entity header file.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement