2014-12-06 26 views
0

我正在使用Boost正則表達式庫的程序,並且遇到了嘗試調用boost :: regex_match()時遇到的問題。函數,我得到一個奇怪的錯誤,我從來沒有遇到過。這是相關的代碼。似乎無法在Boost庫中使用此函數

boost::regex pattern("REGEX REDACTED"); 
boost::cmatch what; 

XERCES_STD_QUALIFIER cout << "Enter XML Document-Building Commands" << XERCES_STD_QUALIFIER endl; 
while(true) { 
    // Take the input from the user. 
    std::string input; 
    XERCES_STD_QUALIFIER cout << ">> "; 
    XERCES_STD_QUALIFIER getline(in, input); 

    if(boost::regex_match(input, what, pattern)) { 
     // whatever 
    } 
} 

這幾乎就是我從我的老師給這個任務提供的類似程序中的代碼中拿出來的。但是當我嘗試編譯時,我得到這個錯誤。如果有幫助,我使用的是NetBeans 8.

XMLDoc.cpp: In member function ‘void XMLDoc::createDoc(std::istream&)’: 
XMLDoc.cpp:164:51: error: no matching function for call to ‘regex_match(std::string&,  boost::cmatch&, boost::regex&)’ 
XMLDoc.cpp:164:51: note: candidates are: 
In file included from /usr/include/boost/regex/v4/regex.hpp:145:0, 
       from /usr/include/boost/regex.hpp:31, 
       from XMLDoc.cpp:13: 
/usr/include/boost/regex/v4/regex_match.hpp:44:6: note: template<class BidiIterator, class Allocator, class charT, class traits> bool boost::regex_match(BidiIterator, BidiIterator, boost::match_results<Iterator, Allocator>&, const boost::basic_regex<charT, traits>&, boost::regex_constants::match_flag_type) 
/usr/include/boost/regex/v4/regex_match.hpp:44:6: note: template argument deduction/substitution failed: 
XMLDoc.cpp:164:51: note: deduced conflicting types for parameter ‘BidiIterator’ (‘std::basic_string<char>’ and ‘boost::match_results<const char*>’) 
In file included from /usr/include/boost/regex/v4/regex.hpp:145:0, 
       from /usr/include/boost/regex.hpp:31, 
       from XMLDoc.cpp:13: 
/usr/include/boost/regex/v4/regex_match.hpp:53:6: note: template<class iterator, class charT, class traits> bool boost::regex_match(iterator, iterator, const boost::basic_regex<charT, traits>&, boost::regex_constants::match_flag_type) 
/usr/include/boost/regex/v4/regex_match.hpp:53:6: note: template argument deduction/substitution failed: 
XMLDoc.cpp:164:51: note: deduced conflicting types for parameter ‘iterator’ (‘std::basic_string<char>’ and ‘boost::match_results<const char*>’) 
In file included from /usr/include/boost/regex/v4/regex.hpp:145:0, 
       from /usr/include/boost/regex.hpp:31, 
       from XMLDoc.cpp:13: 
/usr/include/boost/regex/v4/regex_match.hpp:68:13: note: template<class charT, class Allocator, class traits> bool boost::regex_match(const charT*, boost::match_results<const charT*, Allocator>&, const boost::basic_regex<charT, traits2>&, boost::regex_constants::match_flag_type) 
/usr/include/boost/regex/v4/regex_match.hpp:68:13: note: template argument deduction/substitution failed: 
XMLDoc.cpp:164:51: note: mismatched types ‘const charT*’ and ‘std::basic_string<char>’ 
In file included from /usr/include/boost/regex/v4/regex.hpp:145:0, 
       from /usr/include/boost/regex.hpp:31, 
       from XMLDoc.cpp:13: 
/usr/include/boost/regex/v4/regex_match.hpp:77:13: note: bool boost::regex_match(const std::basic_string<charT, ST, SA>&, boost::match_results<typename std::basic_string<charT, ST, SA>::const_iterator, Allocator>&, const boost::basic_regex<charT, traits>&, boost::regex_constants::match_flag_type) [with ST = std::char_traits<char>; SA = std::allocator<char>; Allocator = std::allocator<boost::sub_match<const char*> >; charT = char; traits = boost::regex_traits<char>; typename std::basic_string<charT, ST, SA>::const_iterator = __gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >; boost::regex_constants::match_flag_type = boost::regex_constants::_match_flags] 
/usr/include/boost/regex/v4/regex_match.hpp:77:13: note: no known conversion for argument 2 from ‘boost::cmatch {aka boost::match_results<const char*>}’ to ‘boost::match_results<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> >, std::allocator<boost::sub_match<const char*> > >&’ 
/usr/include/boost/regex/v4/regex_match.hpp:85:13: note: template<class charT, class traits> bool boost::regex_match(const charT*, const boost::basic_regex<charT, traits>&, boost::regex_constants::match_flag_type) 
/usr/include/boost/regex/v4/regex_match.hpp:85:13: note: template argument deduction/substitution failed: 
XMLDoc.cpp:164:51: note: mismatched types ‘const charT*’ and ‘std::basic_string<char>’ 
In file included from /usr/include/boost/regex/v4/regex.hpp:145:0, 
       from /usr/include/boost/regex.hpp:31, 
       from XMLDoc.cpp:13: 
/usr/include/boost/regex/v4/regex_match.hpp:94:13: note: template<class ST, class SA, class charT, class traits> bool boost::regex_match(const std::basic_string<charT, ST, SA>&, const boost::basic_regex<charT, traits>&, boost::regex_constants::match_flag_type) 
/usr/include/boost/regex/v4/regex_match.hpp:94:13: note: template argument deduction/substitution failed: 
XMLDoc.cpp:164:51: note: ‘boost::cmatch {aka boost::match_results<const char*>}’ is not derived from ‘const boost::basic_regex<charT, traits>’ 

有人可以幫我嗎?我確定我正確地包含了這個庫,因爲我有另外一個項目,它具有完全相同的使用boost :: regex_match函數的相同鏈接器屬性(減去cmatch對象參數),並且它工作得很好。

回答

2

如果您要使用std::string輸入,則必須使用boost::smatch而不是boost::cmatch。請參閱http://www.boost.org/doc/libs/1_57_0/libs/regex/doc/html/boost_regex/ref/match_results.html。因此,所有你需要做的是改變

boost::cmatch what; 

boost::smatch what; 
+0

我試着做input.c_str(),以及,並得到了類似的錯誤。 – 2014-12-06 04:59:16

+0

錯誤是什麼?我可以用boost :: cmatch做input.c_str(),它適用於我。 – bgoldst 2014-12-06 05:10:33

+0

我將它換成了boost :: smatch,並得到了相同的確切錯誤。 – 2014-12-06 06:04:50