首先我應該指出這是我的第一個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 ::正則表達式,而不是一個字符串向量;
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添加重載(或將字符串向量更改爲正則表達式)。
我假設這個問題與我在函數調用中使用boost :: assign有關,我意識到我不需要,但我真的需要'單線'函數調用。 ...任何意見的歡迎,因爲我對C++來說是新手。
感謝
我的個人建議:如果避免語法超載,你的生活會更快樂。 –
感謝您的建議,我想您可能是對的! –