我有一个自定义类型,目前由std :: pair占据。 (在我通过......之前,它会有更多的数据)并从文件中读取它。
我已将代码缩减为:
#include <iostream>
#include <cstdint>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <istream>
using namespace std;
// Show duration isn't an issue, so I can treat all times as integers in the range 0-2400.
using Time = uint16_t;
using Show = pair<Time,Time>; // Placeholder type, for now. This will probably end up as some sort of a std::tuple.
// Build a Show object from its component pieces.
inline Show make_show(Time start, Time finish)
{
return make_pair(start, finish);
}
inline Time& start(Show& show)
{
return show.first;
}
inline Time& finish(Show& show)
{
return show.second;
}
// Get a Show object from a stream.
inline istream& operator>>(istream& is, Show& show)
{
is >> start(show) >> finish(show);
return is;
}
// Get a vector (sorted by start time) of all the shows in an EOF terminated stream
vector<Show> readShows(istream& stream)
{
auto result = vector<Show>{};
// main.cpp, line 58 is the next line.
copy(istream_iterator<Show>(stream), istream_iterator<Show>(), back_inserter(result));
sort(begin(result), end(result));
return result;
}
但是,当我编译它时,我收到一个错误。从本质上讲,错误是抱怨当它试图创建istream_iterator<Show>
对象时,找不到合适的operator>>
:&#34; error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'Show' (or there is no acceptable conversion)
&#34;。
这让我感到困惑,因为我做有一个函数,在程序中声明了更高的签名istream& operator>>(istream& is, Show& show)
。
可以通过在Visual Studio 2013中使用默认的Windows编译器进行编译,或者通过在Clang下编译来生成此错误(它提供&#34; error : invalid operands to binary expression ('istream_type' (aka 'basic_istream<char, std::char_traits<char> >') and 'std::pair<Time, Time>')
&#34; - 基本相同的错误,使用不同的措辞。 )
欢迎任何帮助。
这里给出了完整的错误:
1>------ Build started: Project: ScratchSpace, Configuration: Debug Win32 ------
1> main.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\iterator(256): error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'Show' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(485): could be 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::basic_streambuf<char,std::char_traits<char>> *)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(466): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(void *&)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(448): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(long double &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(430): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(double &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(411): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(float &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(392): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned __int64 &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(373): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(__int64 &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(353): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned long &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(335): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(long &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(317): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned int &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(291): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(int &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(272): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(unsigned short &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(237): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(short &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(218): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::_Bool &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(211): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::ios_base &(__cdecl *)(std::ios_base &))'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(204): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::basic_ios<char,std::char_traits<char>> &(__cdecl *)(std::basic_ios<char,std::char_traits<char>> &))'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(198): or 'std::basic_istream<char,std::char_traits<char>> &std::basic_istream<char,std::char_traits<char>>::operator >>(std::basic_istream<char,std::char_traits<char>> &(__cdecl *)(std::basic_istream<char,std::char_traits<char>> &))'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(1103): or 'std::basic_istream<char,std::char_traits<char>> &std::operator >><char,std::char_traits<char>,_Ty>(std::basic_istream<char,std::char_traits<char>> &&,_Ty &)'
1> with
1> [
1> _Ty=Show
1> ]
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(1093): or 'std::basic_istream<char,std::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,unsigned char &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(1086): or 'std::basic_istream<char,std::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,unsigned char *)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(1079): or 'std::basic_istream<char,std::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,signed char &)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\istream(1072): or 'std::basic_istream<char,std::char_traits<char>> &std::operator >><std::char_traits<char>>(std::basic_istream<char,std::char_traits<char>> &,signed char *)'
1> while trying to match the argument list '(std::basic_istream<char,std::char_traits<char>>, Show)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\iterator(255) : while compiling class template member function 'void std::istream_iterator<Show,char,std::char_traits<char>,ptrdiff_t>::_Getval(void)'
1> c:\program files (x86)\microsoft visual studio 12.0\vc\include\iterator(222) : see reference to function template instantiation 'void std::istream_iterator<Show,char,std::char_traits<char>,ptrdiff_t>::_Getval(void)' being compiled
1> c:\users\gbenglisha\documents\visual studio 2013\projects\scratchspace\scratchspace\main.cpp(58) : see reference to class template instantiation 'std::istream_iterator<Show,char,std::char_traits<char>,ptrdiff_t>' being compiled
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
答案 0 :(得分:0)
在这种情况下,看起来ADL查找失败了。您依靠ADL查找来查找function palindrome(str)
{
str = str.split(' ').join('');
str = str.split(',').join('');
str = str.split('.').join('');
str = str.toLowerCase();
if (str.split('').reverse().join('') == str)
{
return true;
}
else
{
return false;
}
}
palindrome("never odd or even");
和istream
(不在命名空间std::)中的重载,并且无法找到它。可能与pair
模板有关(不知道多少,我知道!)。当使用istream_iterator
而不是istream_iterator算法时,一切运行良好 - 所以如果你没有使用迭代器+副本,你可能可以做一个简单的循环。
修复它的一种可怕方法是在命名空间stream >> show;
中定义operator >>
,但这种技术是不好的理由。应该有其他方法来帮助ADL,但我不知道该怎么做。