Is it possible to declare a string in a function?

May 10, 2012 at 10:36pm
Whenever I declare the function as a string, it gives me the error: "String does not name a type." If I try to return a string it says: "Invalid conversion from 'const char*' to 'int'. If I try to declare a string, it tells me: "String was not declared in this scope". I don't see anyway of doing it. I am using XCode on a mac.
May 10, 2012 at 10:41pm
closed account (zb0S216C)
Gregory Sakas wrote:
"String"

...is not a type defined in C++. However, string is.

Gregory Sakas wrote:
"Invalid conversion from 'const char*' to 'int'"

That means nothing without the code counterpart.

Wazzak
Last edited on May 10, 2012 at 10:42pm
May 10, 2012 at 10:50pm
I know i meant "string" my bad.
May 10, 2012 at 10:54pm
closed account (zb0S216C)
In that case, I can think of two reasons why you're getting the "does not name a type" error:

1) You haven't included the string library (#include <string>)
2) You have included the library, but you haven't qualified string with std (std::string)

Wazzak
May 10, 2012 at 10:54pm
1
2
3
4
5
6
7
8
9
10
11
12
#include <string>
using namespace std;

string makeWord(){
   string word = "hello";
   return word;
}

int main(){
   string word = makeWord();
   return 0;
}


Shouldn't be a problem.
May 10, 2012 at 11:02pm
I forgot to put the using namespace std; in front of the function. Such a stupid mistake. I am literally banging my head against a wall right now.
May 11, 2012 at 12:00am
And also my bad for not thanking you all. Especially LowestOne, who made me realize my rookie mistake.
Topic archived. No new replies allowed.