首先,我应该指出这是我的第一个stackoverflow问题,因此请耐心等待。
我在重载c ++中的函数时遇到一些问题。我正在尝试使用以下原型创建一个函数:
void push_at_command(std::string, std::vector<std::string>, int);
void push_at_command(std::string, std::vector<std::string>, std::vector<std::string>, int);
void push_at_command(std::string, std::vector<std::string>, std::vector<std::string>, std::vector<std::string>, int);
void push_at_command(std::string, std::vector<std::string>, bool, int);
我最初希望最后一个重载(带有布尔值的重载)接受boost :: regex而不是字符串向量;
void push_at_command(std::string, boost::regex, int);
但遇到歧义错误...所以为了快速使代码“正常运行”,我想我将添加一个原型以接受一个标志,并使用向量中的第一个元素存储一个正则表达式字符串,但我似乎遇到类似的布尔值问题。
这就是我试图称呼这些重载的方式:
push_at_command(
"AT?S",
boost::assign::list_of("(\\d{3}.\\d{3})"),
true,
0);
push_at_command(
"AT?S",
boost::assign::list_of("L11")("L12"),
0);
push_at_command(
"AT?S",
boost::assign::list_of("L11"),
boost::assign::list_of("L21")("L22"),
0);
这是我得到的错误:
error: call of overloaded ‘push_at_command(const char [5], boost::assign_detail::generic_list<char [4]>, boost::assign_detail::generic_list<char [4]>, int)’ is ambiguous
note: candidates are:
note: void push_at_command(std::string, std::vector<std::basic_string<char> >, std::vector<std::basic_string<char> >, int)
note: void push_at_command(std::string, std::vector<std::basic_string<char> >, bool, int)
...与第三个函数调用有关。
只是要注意,在使用bool添加重载(或将字符串向量更改为regex)之前,我没有任何问题。
我以为这个问题与在函数调用中使用boost :: assign有关,我意识到我并不需要,但是我确实需要“单行”函数调用。...欢迎任何建议,因为我对C ++还是相当陌生。
谢谢
问题是,如boost文档中所示, But what if we need to initialize a container? This is where list_of() comes into play. With list_of() we can create anonymous lists that automatically converts to any container:
在这种情况下,您不希望能够转换为任何容器,而是需要特定的s向量。由于具有这种可转换类型,因此无法确定应转换为bool还是vector,从而使调用不明确。
如果您确实想继续创建的重载集(请退后一步,然后使用标志重新考虑您的方法),则需要将列表专门分配给向量(我假设此处list_of
提供了将运算符转换为矢量):
push_at_command(
"AT?S",
boost::assign::list_of("L11"),
std::vector<std::string>(boost::assign::list_of("L21")("L22")),
0);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句