J
Jason Heyes
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:air<T,U> &pr)
{
T first;
if (!(is >> first))
return is;
U second;
if (!(is >> second))
return is;
pr = std:air<T,U>(first, second);
return is;
}
template <class T, class U>
std:stream &operator<<(std:stream &os, const std:air<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:air<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:air<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.
#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:air<T,U> &pr)
{
T first;
if (!(is >> first))
return is;
U second;
if (!(is >> second))
return is;
pr = std:air<T,U>(first, second);
return is;
}
template <class T, class U>
std:stream &operator<<(std:stream &os, const std:air<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:air<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:air<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.