user393964
user393964

Reputation:

Pass NSString to C++ method

I'm trying to pass a NSString to a C++ method and I tried this but it's not working:

    trackName = @"name";
    Track track(new String([trackName UTF8String]));

I'm writing this code inside an .mm file. I also added #include <string> at the top of the file but I'm not sure if that's necessary. Any idea what I'm doing wrong? These are the errors that I'm getting:

enter image description here

And this is the C++ track constructor that I'm calling:

Track(const string &aTrackName)
    {
    trackName =  aTrackName;
}

Upvotes: 0

Views: 633

Answers (1)

Ferruccio
Ferruccio

Reputation: 100638

The problem is that your function is expecting a string and you're trying to instantiate a String object, which it probably knows nothing about.

The other problem with this code is that it expects a string reference but you are passing it a pointer to a string. You can simply pass it a string temporary. Try:

Track track(std::string([trackName UTF8String]));

Upvotes: 1

Related Questions