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.

4

2 回答 2

0

呃,boost::bimap本身没有add_values方法,从这些代码片段中很难判断为什么你会突然期待一个方法出现。

于 2012-04-08T19:24:37.227 回答
0

考虑重命名您的函数:调用 get_values() 的 set_values() 调用 add_values() 是一个令人困惑的调用链......

当您需要修改函数中的对象时,您必须通过引用(或指针)来获取它。这个想法是您必须在函数内部和外部使用相同的对象。如果您按值传递,函数将看到一个副本,因此它对它所做的任何事情都不会反映在原始对象上。

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

这就是你将如何称呼它:

initialize(myBitmap, 3, 5);

确保更新您的整个调用链以在适当的情况下通过引用传递,因为目前您的 get_values() 也适用于副本。

于 2012-04-08T19:27:56.887 回答