irr::core::map

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
killthesand
Posts: 42
Joined: Sat Sep 29, 2007 3:33 am
Contact:

irr::core::map

Post by killthesand »

I'm confused on something.
say I have

Code: Select all

map<int, stringc> myMap;
The comments in irrMap.h make it seem like I should be able to do this:

Code: Select all

myMap[5] = "Hello";
stringc hello = myMap[5];
but it doesn't work, the operator[] is defined for AccessClass
I searched the source as best I could and I found no place it's being used this way.
Instead I can do this:

Code: Select all

myMap.set(5, "Hello");
stringc hello = myMap.find(5)->getValue();
How is AccessClass supposed to work?
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

It should work. core::map::operator[](const Key&) returns AccessClass which has a conversion operator to ValueType. Perhaps it would be good if you showed us an error message?

Travis
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

BTW, I think that AccessClass should have these overloads for efficiency...

Code: Select all

AccessClass::operator ValueType&();
AccessClass::operator const ValueType&() const;
Travis
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

Post by DtD »

I've never had luck with it either, the only thing I ever got to work was:

Code: Select all

core::map<core::stringc,core::stringc> tmap;
tmap.set(core::stringc("Irrlicht"),core::stringc("Awesome"));
std::cout <<  tmap.find(core::stringc("Irrlicht"))->getValue().c_str();
But, I have never really liked that way. Also, I think not drop()ing the result from tmap.find(core::stringc("Irrlicht")) would cause a memory leak, but I'm no expert on that.

(Oh, and I got errors doing it without the ->getValue and with the operator[] they had to do with type conversion if I remembered right, but I was using the same types!)

~DtD
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Weird. This works just fine for me (using vanilla Irrlicht 1.5 and Microsoft Visual C++ 2005 (14.00.50727.762)).

Code: Select all

#include <irrlicht.h>
#include <stdio.h>

#pragma comment(lib, "irrlicht.lib");

using namespace irr;

int main ()
{
  core::map<int, core::stringc> m;
  m [1] = "the";
  m [2] = "quick";
  m [3] = "brown";
  m [4] = "fox";

  const core::stringc& s1 = m [1];
  const core::stringc s2 = m [2];
  core::stringc s3 = m [3];

  printf ("1: %s\n2: %s\n3: %s\n",
    s1.c_str(), s2.c_str(), s3.c_str());

  return 0;
}
If you're having trouble, post a simple testcase and tell us what version of Irrlicht and your compiler you are using. That will help to figure out if it is a problem with your compiler, Irrlicht, or your code.

Travis
DtD
Posts: 264
Joined: Mon Aug 11, 2008 7:05 am
Location: Kansas
Contact:

Post by DtD »

Hmm, oddly it works. I did a similar thing with stringc and char* indicies and it works. I'm not sure what I did before that failed but it is working now.

Code: Select all

#include <irrlicht.h>
#include <stdio.h>

#pragma comment(lib, "Irrlicht.lib")

using namespace irr;

int main()
{
 core::map<core::stringc,core::stringc> myMap;
 myMap["Entry1"]="DtD Software";
 myMap["Number2"]="Irrlicht";
 myMap["Three"]="MSVS";
 myMap["Four"]="One more";
 
 const core::stringc& s1 = myMap["Entry1"]; 
 const core::stringc s2 = myMap["Number2"]; 
 core::stringc s3 = myMap["Three"];
 
 printf ("%s\n%s\n%s\n",s1.c_str(),s2.c_str(),s3.c_str());

	return 0;
}
Well, I could go dig though my old backups and try to find a place where I was playing with them before, but it would take forever to find one. BUt it works now for unknown reasons, so thanks :lol:

Running MSVS 2008 Pro with Windows XP and Vanilla Irrlicht 1.5 I ran tests with my non-vinilla modified Irrlicht and it works too.

~DtD
killthesand
Posts: 42
Joined: Sat Sep 29, 2007 3:33 am
Contact:

Post by killthesand »

Well don't I feel foolish. The [] operator wouldn't work for me because I have a pointer to a map. It works beautifully once I dereference it first. Thank you all very much for the assistance.
Post Reply