469,903 Members | 1,808 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

VC++ 6.0 problem.

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.
Jul 22 '05 #1
4 2019
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:40***********************@news.optusnet.com.a u...
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
Jul 22 '05 #2
"P.J. Plauger" <pj*@dinkumware.com> wrote in message
news:zt******************@nwrddc03.gnilink.net...
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:40***********************@news.optusnet.com.a u...
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.


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.
Jul 22 '05 #3
Jason 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
Jul 22 '05 #4
I found the problem. Anyway, thanks for the help.
Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Tatu Portin | last post: by
11 posts views Thread by Zatts | last post: by
3 posts views Thread by Hamilton Woods | last post: by
7 posts views Thread by Norman Diamond | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.