c++ cli - Adding a System::String^ as key in VC++ Dictionary -


i new c++, i'm stuck on i'm sure trivial.

i have dictionary:

dictionary<string^, room^>^ roomlist = gcnew dictionary<string^, room^>(); 

i'm trying add new room dictionary:

room r("room 1", x, y); roomlist->add(r.getname, %r); 

room defined follows:

ref class room {  private: string^ mname; double mx; //scaled x-coordinate of top left corner (meters) double my; //scaled y-coordinate of top left corner (meters)  public: room(string^ name, double x, double y);   string ^ const getname() { return mname; } double const getx() { return mx; } double const gety() { return my; }  }; 

when try compile code following error:

'room::getname': non-standard syntax; use '&' create pointer member"

what doing wrong? reason can't use object's name (a system::string^) key, i'm not sure why.

   roomlist->add(r.getname, %r); 

you declared getname function, not property. needs r.getname(), note added () parentheses. declaring name property wise, .net way.

   room r("room 1", x, y); 

this declaration technically wrong. using stack semantics, r object automatically disposed when code execution leaves scope block. never want add disposed object collection. you'll away in case since did not implement destructor. woe if ever do. , woe reader of code. correctly:

   room^ r = gcnew room("room 1", x, y);    roomlist->add(r=>getname(), r); 

last not least, looks student assignment. cannot passing grade code, not c++. language using called c++/cli, extension language helps writing interop code .net programs.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -