0

OK this is total noob question:

I have simple C++ method:

 void Tray::IconPos(const std::string& iconpos) {
   NSRect rect = [[[status_item_ view] window] frame];
   iconpos = [NSString stringWithFormat: @"%f,%f", rect.origin.x, rect.origin.y];
 }

where I want std::string& iconpos to gain this: [NSString stringWithFormat: @"%f,%f", rect.origin.x, rect.origin.y] value.

How do I do that here?

Ninja is giving me:

tray_mac.mm:72:11: error: no viable overloaded '='
iconpos = [NSString stringWithFormat: @"%f,%f", rect.origin.x, rect.origin.y];
~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tomas Bulva
  • 1,120
  • 1
  • 9
  • 17

2 Answers2

1

First, if you are going to convert NSRect to NSString you probably want NSStringFromRect.

However, if you want to format it yourself, into a std::string, then why convert it to a NSString just to convert it again to a std::string?

Second, you are trying to assign to iconpos which is a reference to a const std::string. You just can't do that.

If you want to convert from NSString to a std::string you will need to go through a regular C string, with the NSString method cStringUsingEncoding:.

However, no matter what you do, you can't assign to iconpos because it is a reference to a constant object.

Jody Hagins
  • 27,943
  • 6
  • 58
  • 87
0

Your should try something like this:

iconpos = [[NSString stringWithFormat: @"%f,%f", rect.origin.x, rect.origin.y] UTF8String];
Joel
  • 4,732
  • 9
  • 39
  • 54
Atomix
  • 13,427
  • 9
  • 38
  • 46
  • still the same error: :-( `../../content/nw/src/api/tray/tray_mac.mm:72:11: error: no viable overloaded '=' iconpos = [[NSString stringWithFormat: @"%f,%f", rect.origin.x, rect.origin.y] UTF8String]; ~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~` – Tomas Bulva Feb 14 '14 at 22:21
  • How about this: `iconpos.assign([[NSString stringWithFormat: @"%f,%f", rect.origin.x, rect.origin.y] UTF8String]);` ? – Atomix Feb 14 '14 at 22:55
  • `../../content/nw/src/api/tray/tray_mac.mm:73:11: error: no matching member function for call to 'assign' iconpos.assign([[NSString stringWithFormat: @"%f,%f", rect.origin.x, rect.origin.y] UTF8String]); ~~~~~~~~^~~~~~ /MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/c++/4.2.1/bits/basic_string.h:927:7: note: candidate function not viable: 'this' argument has type 'const std::string' (aka 'const basic_string'), but method is not marked const assign(const _CharT* __s) ^ 1 error generated. ninja: build stopped: subcommand failed.` – Tomas Bulva Feb 17 '14 at 14:35
  • As Jody Hagins pointed out, you are trying to assign to a const reference. Remove the 'const' from the header declaration. – Atomix Feb 17 '14 at 19:53