0

I'm new to the bimap functionality of the Boost libraries, and I'm having trouble passing a bimap into another function. My bimap looks like this:

typedef boost::bimap< int, int > bimap_type;
bimap_type bm;

I have an add_values() function that adds a set of values to the bimap:

add_values(int a, int b)
{
 bm.insert(bimap_type::value_type(a, b));
}

I then have a function that is meant to set the values of the bimap by getting them from a Singleton Class:

void set_values()
{
 MyClass::instance()->get_values(bm);
}

And, in MyClass, get_values() looks like this:

void get_values(bimap_type myBimap)
{
 myBimap.add_values(3, 5);
}

However, MyClass does not recognise 'bimap_type'. I try putting the typedef in a separate header file and including that in MyClass, but I get the error message:

'class bimap_type' has no member named 'add_values'

How can I successfully pass the bimap to this Singleton Class in order to fill it with values from the Class? Does anyone know?

Thanks a lot.

user1295558
  • 117
  • 9

2 Answers2

0

Er, boost::bimap itself doesn't have an add_values method and it's hard to tell from these code fragments why you're suddenly expecting one to appear.

timday
  • 24,582
  • 12
  • 83
  • 135
  • Sorry, I should have been more clear - add_values() is my own function. For the sake of argument, it could be replaced with the usual insert() function. My question still stands, though - do you know how I would pass the bimap to a function in my Singleton class? – user1295558 Apr 08 '12 at 21:03
  • Thanks both timday and Eugene - I've now got it sorted, using the advice both of you gave. I'm now passing the bimap by reference, and I've scrapped my add_values() function, as it did no more than boost::bimap's insert() function. Cheers for your help. – user1295558 Apr 08 '12 at 21:27
0

Consider renaming your functions: set_values() that calls get_values() that calls add_values() is one confusing call chain...

When you need to modify an object in a function, you have to take it by reference (or a pointer). The idea is that you must work with the same object inside and outside of the function. If you pass by value, function will see a copy, so anything it does with it does not reflect on original object.

// formerly known as add_values()
void initialize(bimap_type& bm, int a, int b)
{
    bm.insert(bimap_type::value_type(a, b));
}

And this is how you will call it:

initialize(myBitmap, 3, 5);

Make sure to update your whole call chain to pass by reference where appropriate, because currently your get_values() works with a copy too.

Eugene
  • 7,180
  • 1
  • 29
  • 36
  • Thanks a lot, I'll try this, and also rename my functions. – user1295558 Apr 08 '12 at 21:05
  • Thanks both timday and Eugene - I've now got it sorted, using the advice both of you gave. I'm now passing the bimap by reference, and I've scrapped my add_values() function, as it did no more than boost::bimap's insert() function. Cheers for your help. – user1295558 Apr 08 '12 at 21:27