Advertisement

The constant that wasn't

Started by November 14, 2000 08:36 PM
5 comments, last by Yanroy 24 years, 1 month ago
Here is one that should stump you. I have been trying to figure it out for about 3 hours now...
  
#pragma once

const int MapTileSize = 32;
const int MapX = 256;
const int MapY = 256;
const int BackgroundX = (MapX / 2) + (SCREEN_WIDTH / MapTileSize);
const int BackgroundY = (MapY / 2) + (SCREEN_HEIGHT / MapTileSize);
const int BackdropX = SCREEN_WIDTH / MapTileSize;
const int BackdropY = SCREEN_HEIGHT / MapTileSize;
const int GridlineTile = 10;

namespace Map
{
	// The Map

	struct MapTile
	{
		BYTE Graphic; // the column of the tile graphic

	};

	MapTile Map[MapX][MapY];
	BYTE Background[BackgroundX][BackgroundY];
	BYTE Backdrop[BackdropX][BackdropY];

	// Viewpoint Stuff

	int ViewpointX = 0;
	int ViewpointY = 0;

	// Misc Stuff

	RECT CurrentTile;
	RECT DestRect;
	//CLinkedList<CUnit> UnitList; // CUnit isn''t finished yet, so ignore it


	// Functions

	void Scroll();
	bool Load(char *Filename);
	void Draw();
	void DrawBackground();
}
  
The Problem: Take a look at the Background and Backdrop declarations... Everything looks fine, right (it worked just yesterday!)? Think again. The compiler says it expected a constant expression on both of those lines. As an expirement, I changed the declaration of BackgroundX and BackgroundY to be MapX and MapY. Then I only had a problem with the Backdrop! I changed the Background constants so that there was no mention of SCREEN_WIDTH or SCREEN_HEIGHT and it worked! SCREEN_HEIGHT and SCREEN_WIDTH are defined as extern const int in Engine.h, and actually defined in Game.cpp, along with other engine-required but user-supplied constants. Everything worked until I took everything out of my headers and put it into cpp files and headers (I should have done that before I started). Everything else made the transition perfectly. It seems to me that BackgroundX and BackgroundY are defined as const, but really aren''t.............. (insert spooky music here) --------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

I don''t think there can be any variable names in a constant declaration, even if those variables themselves are constant. You need use numbers. Or else I don''t know whats wrong .

=======================================
Better to reign in hell than serve in heaven.
John Milton, Paradise Lost
Advertisement
try doing

enum
{
MapTileSize = 32,
MapX = 256,
MapY = 256,
BackgroundX = (MapX / 2) + (SCREEN_WIDTH / MapTileSize),
BackgroundY = (MapY / 2) + (SCREEN_HEIGHT / MapTileSize),
};

and then the other stuff as normal. should work.
Hey, assuming you''re using Visual C++, have you tried deleting all your intermediate files manually and recompiling? I know it''s weird, but I had a problem like this, where it wasn''t compiling but it should have. I deleted the obj files and all the rest, basically the entire contents of the Debug/Release directory, and then rebuilt and it worked.

Good luck!
Interesting... In vc++ I've tried the following and it worked, got value 8 back.

    int main(int argc, char* argv[]){		const int ci = 32;		const int i = ci / 4;			int arr<i>;	cout << sizeof arr / sizeof(arr[0]) << endl;	return 0;}   


I'm thinking something happened during the conversion from headers to cpp files, probably a "const" was left out or something like that.

Edited by - JD on November 15, 2000 1:45:07 AM
Thanks for those replies. I will put all of them to good use, as I get out of school at noon . I did try a complete rebuild by clicking build all, but that didn''t work. I will try manually deleteing the files. When I had everything in my header files, this worked perfectly. I don''t see why it wouldn''t work now. If I left a const behind somewhere, wouldn''t that generate an error?

JD - That little example program does exactly what it *should* do, just like mine did yesterday.

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming


You are unique. Just like everybody else.

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

Advertisement
A variable declared with the extern storage-class specifier is a reference to a variable with the same name defined at the external level in any of the source files of the program. Because the actual value of the reference isn''t known at compile time (only at link time) the extern const cannot be considered a constant. In that case it''s only a variable reference to an unspecified value that cannot be modified. If you want to compile the application so that the variable is considered a constant value across all implementation files, consider using static instead of extern in the header file declarations of your consts.

This topic is closed to new replies.

Advertisement