Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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.



Report this thread to moderator Post Follow-up to this message
Old Post
Jason Heyes
06-13-04 08:56 AM


Re: VC++ 6.0 problem.
"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



Report this thread to moderator Post Follow-up to this message
Old Post
P.J. Plauger
06-13-04 08:56 AM


Re: VC++ 6.0 problem.
"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.



Report this thread to moderator Post Follow-up to this message
Old Post
Jason Heyes
06-13-04 08:56 AM


Re: VC++ 6.0 problem.
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

Report this thread to moderator Post Follow-up to this message
Old Post
Kai-Uwe Bux
06-13-04 08:56 AM


Re: VC++ 6.0 problem.
I found the problem. Anyway, thanks for the help.



Report this thread to moderator Post Follow-up to this message
Old Post
Jason Heyes
06-13-04 08:58 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

C++ archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:18 AM.

 

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.