Code Comments
Programming Forum and web based access to our favorite programming groups.Stupid templates won't compile. Here is my program:
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <map>
using namespace std;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T,U> &pr)
{
T first;
if (!(is >> first))
return is;
U second;
if (!(is >> second))
return is;
pr = std::pair<T,U>(first, second);
return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T,U> &pr)
{
return os << pr.first << pr.second;
}
// reads an instance of std::map from an opened file and then writes it
// to std::cout.
int main()
{
ifstream in("test.txt");
map<char,int> m;
typedef istream_iterator<pair<char,int> > InIt;
copy(InIt(in), InIt(), inserter(m, m.begin()));
typedef ostream_iterator<pair<char,int> > OutIt;
copy(m.begin(), m.end(), OutIt(cout, "\n"));
return 0;
}
Here is the compiler error:
--------------------Configuration: Examples - Win32
Debug--------------------
Compiling...
Main.cpp
c:\program files\microsoft visual studio\vc98\include\iterator(176) : error
C2679: binary '>>' : no operator defined which takes a right-hand operand of
type 'struct std::pair<char,int>' (or there is no acceptable conversion)
c:\program files\microsoft visual studio\vc98\include\iterator(176)
: while compiling class-template member function 'void __thiscall
std::istream_iterator<struct std::pair<char,int>,char,struct
std::char_traits<char> >::_Getval(void)'
Error executing cl.exe.
Examples.exe - 1 error(s), 0 warning(s)
I suspect this is a problem with the VC++ 6.0 compiler. How do I work around
it? Thanks.
Post Follow-up to this message"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message news:40cbaf1f$0$21097$afc38c87@news.optusnet.com.au... > I suspect this is a problem with the VC++ 6.0 compiler. How do I work around > it? Thanks. Your suspicion is wrong. Try defining an extractor for template class pair. P.J. Plauger Dinkumware, Ltd. http://www.dinkumware.com
Post Follow-up to this message"P.J. Plauger" <pjp@dinkumware.com> wrote in message
news:ztOyc.15504$tA6.6206@nwrddc03.gnilink.net...
> "Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
> news:40cbaf1f$0$21097$afc38c87@news.optusnet.com.au...
>
> around
>
> Your suspicion is wrong. Try defining an extractor for template
> class pair.
>
Then why does this code compile and run?
#include <fstream>
#include <iostream>
#include <algorithm>
#include <iterator>
#include <map>
using namespace std;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T,U> &pr)
{
return is >> first >> second;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T,U> &pr)
{
return os << pr.first << pr.second;
}
int main()
{
ifstream in("test.txt");
pair<char,int> pr;
in >> pr;
cout << pr;
return 0;
}
There is nothing to stop me from defining an extractor for pair.
Post Follow-up to this messageJason Heyes wrote:
> Stupid templates won't compile. Here is my program:
>
> #include <fstream>
> #include <iostream>
> #include <algorithm>
> #include <iterator>
> #include <map>
> using namespace std;
>
> template <class T, class U>
> std::istream &operator>>(std::istream &is, std::pair<T,U> &pr)
> {
> T first;
> if (!(is >> first))
> return is;
>
> U second;
> if (!(is >> second))
> return is;
>
> pr = std::pair<T,U>(first, second);
> return is;
> }
>
> template <class T, class U>
> std::ostream &operator<<(std::ostream &os, const std::pair<T,U> &pr)
> {
> return os << pr.first << pr.second;
^^
Maybe you want
return os << pr.first << " " << pr.second;
> }
>
> // reads an instance of std::map from an opened file and then writes it
> // to std::cout.
> int main()
> {
> ifstream in("test.txt");
> map<char,int> m;
>
> typedef istream_iterator<pair<char,int> > InIt;
> copy(InIt(in), InIt(), inserter(m, m.begin()));
>
> typedef ostream_iterator<pair<char,int> > OutIt;
> copy(m.begin(), m.end(), OutIt(cout, "\n"));
> return 0;
> }
>
> Here is the compiler error:
>
> --------------------Configuration: Examples - Win32
> Debug--------------------
> Compiling...
> Main.cpp
> c:\program files\microsoft visual studio\vc98\include\iterator(176) :
> error C2679: binary '>>' : no operator defined which takes a right-hand
> operand of type 'struct std::pair<char,int>' (or there is no acceptable
> conversion)
> c:\program files\microsoft visual
> studio\vc98\include\iterator(176)
> : while compiling class-template member function 'void __thiscall
> std::istream_iterator<struct std::pair<char,int>,char,struct
> std::char_traits<char> >::_Getval(void)'
> Error executing cl.exe.
>
> Examples.exe - 1 error(s), 0 warning(s)
>
>
> I suspect this is a problem with the VC++ 6.0 compiler. How do I work
> around it? Thanks.
On my machine your templates compile just fine:
#include <iostream>
#include <algorithm>
#include <iterator>
#include <map>
using namespace std;
template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T,U> &pr)
{
T first;
if (!(is >> first))
return is;
U second;
if (!(is >> second))
return is;
pr = std::pair<T,U>(first, second);
return is;
}
template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T,U> &pr)
{
return os << pr.first << pr.second;
}
int main()
{
pair< char, int > p;
cin >> p;
cout << p;
}
So, I think the problem is in main(). Don't know if that helps.
Best
Kai-Uwe
Post Follow-up to this messageI found the problem. Anyway, thanks for the help.
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.