Skip to main content
GameDev.net gamedev.net
🔒 Locked

Won't Recognize String

Started by WhoCares357 Jul 19, 2008 at 3:39 PM 3 replies 800+ views
Original Post
WhoCares357
WhoCares357
I'm trying to declare a string and then use it later in the script.
string directionG;//global declaration
In the main loop I do:
	directionG = "right";//default declaration
Then in a function (that I later use in the main loop):
	static float indexX = -1.0f;
	static float indexY = -0.3f;
	static float indexZ = -2.0f;

	if (indexX >= 2.0f)
		directionG = "up";
	if (indexX <= -2.0f)
		directionG = "down";
	if (indexZ >= 2.0f)
		directionG = "left";
	if (indexZ <= -2.0f)
		directionG = "right";

	if (directionG == "up")
		indexZ += 0.03f;
	if (directionG == "down")
		indexZ += -0.03f;
	if (directionG == "left")
		indexX += -0.03f;
	if (directionG == "right")
		indexX += 0.03f;
I don't think I'm doing anything wrong but I get this error:
1>e:...\main.cpp(27) : error C2146: syntax error : missing ';' before identifier 'directionG'
1>e:\...\main.cpp(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>e:\...\main.cpp(27) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
The other errors have to do with not being able to convert a char to an integer. Any ideas?
Knuckler
Knuckler
#include

string is part of the std namespace, so you need to qualify it - std::string or
have a using directive in your code - using std::string?
ukdeveloper
ukdeveloper
Quote:
Original post by Knuckler
#include

string is part of the std namespace, so you need to qualify it - std::string or
have a using directive in your code - using std::string?


If that were the case, the C2146 wouldn't necessarily be the first error he saw unless he's not told us everything.

Out of what you've posted, which one exactly is line 27? Could you mark it for us? I need to see whether it's the first time directionG is used or a later one.

WhoCares357
WhoCares357
The error is fixed. I just added the include. Thanks for the help.
visitor
visitor
I don't see you including anywhere.

IMO, using a string for this is a pessimization (unless you have a very strong reason). Why not an enum:

enum Directions { up, down, left, right };


Another thing is: why not do the increment directly where you detect the condition first (unless you want to disable diagonal movement, but even then you could use an else if chain).

And finally, movement could be a vector (e.g a pair of x and y increment) that you could add to your indexN variables without a lot of if's.

Topic Locked

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

Sign in to reply to this topic.