4

Possible Duplicate:
How to convert this code to use string

I have a function like this:

char *foo()
{

}

How can I make it return a string instead? I tried

string foo()
{

}

but the compiler complains.

Community
  • 1
  • 1
node ninja
  • 31,796
  • 59
  • 166
  • 254

2 Answers2

18

Did you do this:

#include <string>
using std::string;

And additionnally, do you use gcc or g++? Even if gcc now can compile C++ code, it is advised to use g++.

Benoit
  • 76,634
  • 23
  • 210
  • 236
  • I forgot the using std::string. That was the problem. – node ninja Sep 25 '10 at 08:01
  • 1
    [`-1` for suggesting a using declaration.](http://stackoverflow.com/questions/2879555/c-stl-how-to-write-wrappers-for-cout-cerr-cin-and-endl/2880136#2880136) – sbi Sep 25 '10 at 09:58
  • 1
    this is not like suggesting a using namespace, heh. – Benoit Sep 25 '10 at 10:23
  • The only thing you did forget is to type "std::" in front of standard library classes/functions. Don't take the habit of "using std::string;" or "using namespace" everywhere. ( especially inside .h header files ) – Nikko Sep 27 '10 at 05:54
7

Try std::string. Standard features are in the std:: namespace.

std::string foo() 
{ 

} 

Be careful with "using" directives, especially in header files. Better take the habit to use std::

Nikko
  • 4,182
  • 1
  • 26
  • 44