Skip to main content
gamedev.net
🔒 Locked

Static Variables

Started by Ataru 3 replies 1k views
Original Post
Ataru
Ataru
Having trouble with a static variable declaration, here''s my class
class terrain_patch
{

	public:	
		//////////////////////////////////////////////////////////////////////////

		// Constructors / Destructors

		terrain_patch() : renderTex(true), renderLighting(true), renderPoly(true)
		{
			tGen = textureManager();
			T_data = new t_renderData[T_length * T_length];
			T_length = 257;
		}

		terrain_patch(int type) : renderTex(true), renderLighting(true), renderPoly(true)
		{
			tGen = textureManager();
			switch (type)
			{
				case T_33x33: T_length = 33; break;
				case T_65x65: T_length = 65; break;
				case T_129x129: T_length = 129; break;
				case T_257x257: T_length = 257; break;
			}
			T_type = type;
			T_data = new t_renderData[T_length * T_length];
		}

		~terrain_patch()
		{
			delete[] T_data;
		}

		void initialize(int type);
		///////////////////////////////////////////////////////////////////////////

		//Assign a value to the terrain

		void setVal(const int x, const int y, const float value) 
		{
			assert ((x < T_length) && (y < T_length) && (x >= 0) && (y >= 0));
			T_data[(y * T_length) + x].height = value;
		}

		//Get the value from the terrain

		float getVal(const int x, const int y) const
		{
			assert ((x < T_length) && (y < T_length) && (x >= 0) && (y >= 0));
			return T_data[(y * T_length) + x].height;
		}

		int __fastcall getLength() const { return T_length; }

		void render();
		void initTerrain();

	protected: 

		int T_type;			//Terrain size flag

		int T_length;		//Terrain length


	private:
		
		void compute_color();
		void compute_texCoord();
		void glVertexRender(int x, int y);

		t_renderData *T_data;		//Terrain data


		bool renderTex;
		bool renderLighting;
		bool renderPoly;
		static textureManager tGen;    //The problem is here

		unsigned int tTex;
};
Well, I can''t seem to get the static decleration of tGen to work. I don''t usualy use them, but in this case I need to, and something is obviously wrong. I get a " rror LNK2001: unresolved external symbol "private: static class textureManager terrain_patch::tGen" (?tGen@terrain_patch@@0VtextureManager@@A) Debug/rSim.exe : fatal error LNK1120: 1 unresolved externals " error. Any idea what I''m doing wrong?
fizban75
fizban75
You have to initialize the static variable outside of the class somewhere.

In your cpp file, add the line:

textureManager terrain_patch::tGen;

Static variables are created on the stack and initialized at program startup. Therefore you need to tell the compiler how to initialize them. The above line will initialize the tGen object with the textureManager default constructor. If you don''t have a default constructor, you''ll need to use one of the other constructors and fill in the appropriate parameters:

textureManager terrain_patch::tGen(var1, var2, var3, etc);

Hope that helps.
superdeveloper
superdeveloper
in your "terrain_path.cpp" file add:

textureManager terrain_patch::tGen;

This will actually create and initialize (if you wish) the actual static var...

http://www.cppnow.com
Ataru
Ataru
Cool it worked, but mostly it makes sense. It''s just like declaring a global variable, I should have thought about it. Thanks guys, both of you.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.