How to define string parameters in C++ header file - Bridged with Swift?


I have a Xcode project with Swift and C++ (with bridging header), and I am trying to create a function in C++ with 2 string parameters, and returning an array of integers back to Swift:

<strong>.cpp file:</strong>

int* example(string one, string two) { int test[3] = {7,2,3}; return test; }

<strong>.h file:</strong>

#if __cplusplus extern "C" { #endif int* example(string one, string two); //ERROR HERE #ifdef __cplusplus } #endif

I am getting this error (in the .h file):


Unknown type name 'string'


I tried to include the string header file to the header, but then I get the error (I also had to add std:: before the string):

<strong>New function declaration:</strong>

int* example(std::string one, std::string two);

<strong>Error (at the first std::)</strong>


Expected ')'


I am really losing my patience here, what am I doing wrong?


You can't use class types in extern "C" declaration. You can introduce the following trick:


#if __cplusplus extern "C" { #endif int* example(void *one, void *two); #ifdef __cplusplus } #endif

source: (.cpp assumed)

#include <string> using namespace std; extern "C" { int* example(void *one, void *two) { string &a = *reinterpret_cast<string*>(one); string &b = *reinterpret_cast<string*>(two); ... } }

You must pass strings by ptr.


  • Another java.lang.UnsatisfiedLinkError with JNI
  • Compiling and linking C and C++ files
  • Wrapping a C lib with extern “C” except an internal C++ include
  • Flot returns incorrect x value (mm/dd/yy - date) after zoom for stack bar chart
  • 'Enemy' was not declared in this scope?
  • Java: java.util.ConcurrentModificationException
  • Boost binary serialization doesn't work occasionally. The parsed data is corrupted sometimes
  • Appium MobileElement swipe returns unknown server error
  • PayPal API Listener Website Payments Standard URI
  • redirect_to root_url and return unless @user.activated
  • Building Qt project for C++11 standard
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deselecting radio buttons while keeping the View Model in synch
  • JSON with duplicate key names losing information when parsed
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Build own AppleScript numerical error handling
  • Websockets service method fails during R startup
  • Jquery - Jquery Wysiwyg return html as a string
  • Google cloud sdk not working when python points python3
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Is there a mandatory requirement to switch app.yaml?
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Hits per day in Google Big Query
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • coudnt use logback because of log4j
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?