mercredi 25 novembre 2015

Error C2679 using a custom extraction operator

I have a custom type, currently placeheld by a std::pair. (It will have more data on it before I'm through...) and am reading it in from a file.

I have cut my code down to:

#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;
}

However, when I compile this I get an error. In essence the error is complaining that when it tries to create an istream_iterator<Show> object it cannot find a suitable operator>>: "error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'Show' (or there is no acceptable conversion)".

This is confusing me, since I do have a function with the signature istream& operator>>(istream& is, Show& show) declared higher up in the program.

This error can be reproduced by compiling with the default Windows compiler in Visual Studio 2013, or by compiling under Clang (which gives "error : invalid operands to binary expression ('istream_type' (aka 'basic_istream<char, std::char_traits<char> >') and 'std::pair<Time, Time>')" - basically the same error with different wording.)

Any help would be welcome.

The full error is given here:

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 ==========

Aucun commentaire:

Enregistrer un commentaire