Jump to content
  • Advertisement
Sign in to follow this  
WhoCares357

Won't Recognize String

This topic is 3679 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
#include <string>

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?

Share this post


Link to post
Share on other sites
Quote:
Original post by Knuckler
#include <string>

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.

Share this post


Link to post
Share on other sites
I don't see you including <string> 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!