【发布时间】:2015-01-20 14:39:38
【问题描述】:
我将以下文件另存为 first.cpp
#include <iostream>
#include <string>
#include <regex>
using namespace std;
int main (){
regex filenameRegex("[a-zA-Z_][a-zA-Z_0-9]*\\.[a-zA-Z0-9]+");
string s2 = "Filenames are readme.txt and my.cmd.";
sregex_iterator it(s2.begin(), s2.end(), filenameRegex);
sregex_iterator it_end;
while(it != it_end){
cout << (*it) << endl;
++it;
}
return 0;
}
尝试使用命令 g++ first.cpp -o first -std=c++11 编译它会产生以下错误:
first.cpp: In function ‘int main()’:
first.cpp:16:15: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
cout << (*it) << endl;
^
In file included from /usr/include/c++/4.9/iostream:39:0,
from first.cpp:1:
/usr/include/c++/4.9/ostream:602:5: note: initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = std::match_results<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> > >]’
operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
我对这里发生的事情感到非常困惑。教程here 声明尊重 sregex_iterator 对象应该返回一个字符串。这不是真的吗?
【问题讨论】:
-
教程错了。