VC++ 6.0 problem.

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::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::eek:stream &operator<<(std::eek:stream &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.
 
J

Jason Heyes

P.J. Plauger said:
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::eek:stream &operator<<(std::eek:stream &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.
 
K

Kai-Uwe Bux

Jason said:
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::eek:stream &operator<<(std::eek:stream &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::eek:stream &operator<<(std::eek:stream &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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top