Sign in to follow this  
Crypted

annoying error regarding strings

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this