Is it possible to declare a string in a function?

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.
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
I know i meant "string" my bad.
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
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.
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.
And also my bad for not thanking you all. Especially LowestOne, who made me realize my rookie mistake.
Topic archived. No new replies allowed.