Re: compile error about auto_ptr
* George:
I am confused to read what the compiler says. Could anyone explain what it
means?
[Code]
#include <memory>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main( )
{
int i = 0;
vector<auto_ptr<int>> vc;
In C++0x you will be able to write ">>", but as of the current standard
you must write "> >".
for (i = 0; i < 5; i ++) // 0 1 2 3 4
{
auto_ptr<int> pi (new int(i));
vc.push_back (pi);
}
return 0;
}
[/Code]
1>Compiling...
1>main.cpp
1>d:\program files\microsoft visual studio 9.0\vc\include\vector(1209) :
error C2558: class 'std::auto_ptr<_Ty>' : no copy constructor available or
copy constructor is declared 'explicit'
This means that no copy constructor is available or the copy constructor
is declared 'explicit', for class 'std::auto_ptr<_Ty>'.
Evidently a copy constructor must be available.
Now, that wasn't so difficult, was it?
1> with
1> [
1> _Ty=int
1> ]
This says the type '_Ty' referred to earlier is actually 'int'.
I.e., there's no available copy constructor for 'std::auto_ptr<int>'.
1> d:\program files\microsoft visual studio
9.0\vc\include\vector(1158) : while compiling class template member function
'void
std::vector<_Ty>::_Insert_n(std::_Vector_const_iterator<_Ty,_Alloc>,unsigned
int,const _Ty &)'
1> with
1> [
1> _Ty=std::auto_ptr<int>,
1> _Alloc=std::allocator<std::auto_ptr<int>>
1> ]
This tells you that it discovered the earlier problem while compiling
the std::vector::_Insert_n member function.
The underscore at the start of the name tells you that it is an internal
implementation detail, not a member function you have called directly.
1> d:\visual studio
2008\projects\test_autoptr1\test_autoptr1\main.cpp(11) : see reference to
class template instantiation 'std::vector<_Ty>' being compiled
1> with
1> [
1> _Ty=std::auto_ptr<int>
1> ]
This offers you even more information the problem: it seems you have
instantiated std::vector, a standard container class template, with type
'std::auto_ptr<int>'.
Or, in the compiler's words, see reference to the class template
instantiation 'std::vector< std::auto_ptr< int > >' being compiled.
Silly.
Element types for standard containers must be copy-constructible.
Considering that std::auto_ptr is all about transferring ownership when
you attempt to copy it, do you think it's copy-constructible?
1>d:\program files\microsoft visual studio 9.0\vc\include\vector(1233) :
error C2558: class 'std::auto_ptr<_Ty>' : no copy constructor available or
copy constructor is declared 'explicit'
1> with
1> [
1> _Ty=int
1> ]
For some reason the compiler now again informs you that there's no copy
constructor available for your chosen std::vector element type.
thanks in advance,
As you can see above, all that's required is to /read/ the error messages.
Take one little bit at a time.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?