Need help with template problem.

J

Jason Heyes

My main program

#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include "M.h"
using namespace std;

int main()
{
vector<M> v;
typedef istream_iterator<auto_ptr<M> > InIt;
copy(InIt(cin), InIt(), back_inserter(v));
return 0;
}

will not compile under VC++ 6.0. It says

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 'class std::auto_ptr<class M>' (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<class std::auto_ptr<class M>,char,struct
std::char_traits<char> >::_Getval(void)'
Error executing cl.exe.

Examples.exe - 1 error(s), 0 warning(s)

but look at M.cpp

#include "M.h"
using namespace std;

istream &operator>>(istream &is, auto_ptr<M> &m)
{
char c;
is >> c;
m = auto_ptr<M>(new M);
return is;
}

and M.h

#include <memory>
#include <istream>

class M
{
public:
M() {}
M(const std::auto_ptr<M> &m) { }
friend std::istream &operator>>(std::istream &is,
std::auto_ptr<M> &m);
};

What's weird is that if I swap around these two lines in my main program

#include "M.h"
using namespace std;

so that it reads as

using namespace std;
#include "M.h"

then everything compiles fine. Please help! I have been trying to understand
this for hours...
 
D

David Harmon

On Thu, 22 Apr 2004 00:48:22 +1000 in comp.lang.c++, "Jason Heyes"
What's weird is that if I swap around these two lines in my main program
so that it reads as

using namespace std;
#include "M.h"

then everything compiles fine.

I think maybe you fooled yourself about "everything compiles fine" as I
get the same error in that case. Looks like perhaps yet another MSVC
name/namespace lookup bug, beats me. If not, perhaps start with the
"everything compiles fine" version and /Fa and look at what mangled
names are generated for operator>>.

In any case, a fundamental problem is that std::auto_ptr<> and any class
containing it has semantics unsuitable for use within a STL container,
so that part of your example is doomed from the start.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top