modusCell
modusCell

Reputation: 13429

Declaring function returning string in C++

i am trying to declare function returning string in header file because i will call this from objective-c. basically this would work, isn't it?

std::string myFunction();

but it throws error message says "Expected ';' after top level declarator", searched a lot, everyones suggest put #include in header files, i tried that as well however it is not working, this time it throws another error message "'string' file not found".

have another function returns double and have no problem with it.

double doSomething(double a);

-

#include <string>

does not work it is throwing error message saying "'string' file not found" . have tried to create new project just in case mine could be damaged but it is not working should i put something in search paths etc?

enter image description here

at last i made it. The solution: have changed "Compile Source As" settings to Objective-C++ under Build Settings / Apple LLVM Compiler 4.2 and it worked like a charm.

Upvotes: 1

Views: 1059

Answers (3)

Psycho
Psycho

Reputation: 1883

You have to remember that ObjC files are built upon C not C++, if you want to use a C++ file in Objective-C you need to change the extension of the ObjC file from .m to .mm to make it an ObjC++ file, or else it will be like trying to include C++ headers in C files.

Upvotes: 1

taocp
taocp

Reputation: 23634

You missed the header file:

  #include <string>

Upvotes: 1

juanchopanza
juanchopanza

Reputation: 227390

At the very least, you have to include the C++ standard library header string, where class std::string is declared (in actual fact, it is a typedef to a class template, but that is another matter).

#include <string>

std::string myFunction();

You should also make sure to use include guards in your own headers.

Upvotes: 3

Related Questions