Returning a string

There is no possible way to return a string right? I tried it with no luck with a couple of variations.

1
2
3
4
5
6
#include <string>
std::string function(){
std::string Foo;
Foo = "Foo";
return Foo;
}
Last edited on
No way.
Function main is declared as returning int. Why are you trying to return a string?!
closed account (o1vk4iN6)
1
2
3
4
5
6
#include <string>

std::string foo()
{
     return "magic";
}
Out of interest, what are you hoping to return the string to?

Andy
Perhaps you just want to print the string:

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

int main()
  {
  std::string message = "Hello world!";
  std::cout << message << std::endl;
  return 0;
  }
C:\Prog> a.exe
Hello world!
C:\Prog> a.exe > foo.txt
C:\Prog> type foo.txt
Hello world!
C:\Prog>

Good luck!
Ok Epic facepalm on my part, I was supposed to say string function();

But I wanted to just return a string variable, I just decided to use pointers.

I just wanted to know why you couldn't return a string, it's part of the STL so It should be included Right?

I need to drink more coffee :)
Last edited on
closed account (DSLq5Di1)
But I wanted to just return a string variable
Yes it's possible, and the edited code in your first post will do just that.

I just decided to use pointers.
I hope you are not returning a pointer to a local variable.
Topic archived. No new replies allowed.