473,396 Members | 2,092 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 2353
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: N4M | last post by:
Dear, I have codes as follows: template<class T> class A { public: // embedded class class E; public: // types typedef E TE; public:// member functions TE somefunc();
11
by: Tatu Portin | last post by:
Have this kind of struct: typedef struct { char **user_comments; /* ... */ } vorbis_comment; /* prototype */ char * read_vorbis_string ( FILE *sc);
5
by: Hari | last post by:
Guys please help me to solve this strange problem what Iam getting as follows.. Trying to instantiate a global instance of a template class as follows :- when i build this code with debug and...
11
by: Zatts | last post by:
I just recently got my hands on a copy of Visual Studio .NET 2003. I have been learning VB.NET for a few months now, but I want to move on from that and start C++.NET. More to the point, does...
1
by: Robert Ludewig | last post by:
I have sucessfully imported and compiled a complex MFC 6.0 project from vc++6.0 (MFC6.0) into vc++ 8.0 (MFC 8.0). It contains several subprojects (libs and dlls). In vc++ 6.0 those project linked...
0
by: parekh | last post by:
Hi All , I am facing a problem wherein my VB project is not recognizing a change in the argument list of a method ( the method itself being declared and defined in another VC++ project and it...
5
by: Andy | last post by:
I'm having trouble accessing an unmanaged long from a managed class in VC++.NET When I do, the contents of the variable seem to be mangled. If I access the same variable byte-by-byte, I get the...
17
by: Fabry | last post by:
Hi All, I'm new of this group and I do not know if this is the correct group for my question. I have a DLL with its export library (.lib) wrote in Borland C++ 6. In borland everything is OK and...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
7
by: Norman Diamond | last post by:
A project depends on VC runtime from Visual Studio 2005 SP1, and DotNet Framework 2. Options are set in the setup project properties, so if these two dependencies are not already installed then...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.