void webparser(const string siteurl,string filename)
{
stringstream ss;
ss << "lynx -dump '" << siteurl << "'" > filename;
system(ss.str().c_str());
}
如何在上例中使用filename而不是file.txt?
最初我对ss的界限是
ss << "lynx -dump '" << siteurl << "' > file.txt";
但我决定改变它以使用参数中的值,所以我添加了文件名。
以下是我的错误消息
main.cpp: In function ‘void webparser(std::string, std::string)’:
main.cpp:244:46: error: no match for ‘operator>’ in ‘std::operator<< [with _Traits = std::char_traits<char>]((* & std::operator<< [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]((* & std::operator<< [with _Traits = std::char_traits<char>]((* & ss.std::basic_stringstream<char>::<anonymous>.std::basic_iostream<char>::<anonymous>), ((const char*)"lynx -dump \'"))), (* & siteurl))), ((const char*)"\'")) > filename’
main.cpp:244:46: note: candidates are:
/usr/include/c++/4.6/bits/stl_pair.h:220:5: note: template<class _T1, class _T2> bool std::operator>(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:303:5: note: template<class _Iterator> bool std::operator>(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:353:5: note: template<class _IteratorL, class _IteratorR> bool std::operator>(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/basic_string.h:2547:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2559:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/basic_string.h:2571:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator>(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/stl_iterator.h:842:5: note: template<class _Iterator, class _Container> bool __gnu_cxx::operator>(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
/usr/include/c++/4.6/bits/stl_iterator.h:836:5: note: template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator>(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)
make: *** [main.o] Error 1
答案 0 :(得分:8)
替换
ss << "lynx -dump '" << siteurl << "'" > filename;
与
ss << "lynx -dump '" << siteurl << "'" << filename;
或者,与原始代码保持一致:
ss << "lynx -dump '" << siteurl << "' > " << filename;