Jump to content
  • Advertisement
Sign in to follow this  
Crypted

annoying error regarding strings

This topic is 3721 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 have this code: #include <cstdlib> #include <iostream> #include <string> string Username() { return "test"; } using namespace std; int main(int argc, char *argv[]) { cout << Username() << endl; system("PAUSE"); return EXIT_SUCCESS; } and I get the error, `string' does not name a type I looked around more and found a different thread about this, http://www.gamedev.net/community/forums/topic.asp?topic_id=408832 but changing the code to: "using namespace std::string;" doesn't change anything. Any help?

Share this post


Link to post
Share on other sites
Advertisement
You need it before its use:
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;

string Username() {
return "test";
}

int main(int argc, char *argv[])
{
cout << Username() << endl;
system("PAUSE");
return EXIT_SUCCESS;
}


or you can:
std::string Username() {
return "test";
}

Share this post


Link to post
Share on other sites
You're declaring your string BEFORE your 'using namespace std'.

Move it down, or explicitly use the namespace like so:


std::string Something = "Hello";




EDIT: Beaten to it, as per bloody usual :)

Share this post


Link to post
Share on other sites
You aren't properly referencing the function; it can't find "string" in namespace std until you use "using namespace std;". Move the Username() function after the using statement and it should be fine.

Keep in mind that "using namespace std;" is a debatable practice and while at the moment it's fine because you aren't doing anything particularly straining, you will want to reevaluate that practice later on.

EDIT: Bah! Beaten as well.

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!