Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
    
Can you show us your attempted #include? –  Joseph Mansfield Apr 7 '13 at 15:17
2  
If your compiler doesn't find <string> then either you are not using a C++-compiler (e.g. gcc can behave like a pure C compiler depending on its arguments), or you have a faulty installation or misconfigured your IDE. Look for the file string manually on your machine (it has no filename suffix), and check the include file path configured in your IDE. –  Arne Mertz Apr 7 '13 at 15:26
    
#ifdef __cplusplus extern "C"..... works, this why assume it is not compiler issue. but i noticed something. if it is pure C++ project no issue, when i link C++ header to Obj-c it starts throwing the error about <string>. –  mohacs Apr 7 '13 at 16:32

3 Answers 3

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.

share|improve this answer

You missed the header file:

  #include <string>
share|improve this answer

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.

share|improve this answer
    
yes i changed extension to .mm –  mohacs Apr 7 '13 at 15:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.