这个程序为什么出错?

lonwind 2002-12-04 10:18:41
#include <iostream>
#include <vector>

using namespace std;

class X{
int i;
};

template<class T> class Node{
public:
auto_ptr<T> pdata;
};

int main(){
vector< Node<X> > f(10);//我想开始向量中有十个Node;
return 0;
}

我拿g++编译的时候出现了如下的错误:
/usr/include/c++/3.2/bits/stl_construct.h: In function `void
std::_Construct(_T1*, const _T2&) [with _T1 = Node<X>, _T2 = Node<X>]':
/usr/include/c++/3.2/bits/stl_uninitialized.h:191: instantiated from `_ForwardIter std::__uninitialized_fill_n_aux(_ForwardIter, _Size,
const _Tp&, __false_type) [with _ForwardIter = Node<X>*, _Size = unsigned int, _Tp = Node<X>]'
/usr/include/c++/3.2/bits/stl_uninitialized.h:216: instantiated from `_ForwardIter std::uninitialized_fill_n(_ForwardIter, _Size, const
_Tp&) [with _ForwardIter = Node<X>*, _Size = unsigned int, _Tp = Node<X>]'
/usr/include/c++/3.2/bits/stl_vector.h:342: instantiated from `std::vector<_Tp, _Alloc>::vector(unsigned int) [with _Tp = Node<X>, _Alloc = std::allocator<Node<X> >]'
test.cpp:17: instantiated from here
/usr/include/c++/3.2/bits/stl_construct.h:78: no matching function for call to
`Node<X>::Node(const Node<X>&)'
/usr/include/c++/3.2/bits/stl_vector.h:342: candidates are: Node<X>::Node()
test.cpp:10: Node<X>::Node(Node<X>&)

不知那位高手能帮我解决一下.
...全文
35 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
shornmao 2002-12-16
  • 打赏
  • 举报
回复
to dragonfly(猴子) :
问题在于,即使提供了public的default ctor,他的程序仍然不会正确,仅仅是可以通过编译而已,所以设计完全就是错误的,有没有default ctor仅仅是一个小小的语法错误罢了。
别的不说,你让他在defautl ctor里怎么初始化auto_ptr呢,auto_ptr根本就没有default ctor。
dragonfly 2002-12-16
  • 打赏
  • 举报
回复
归根到底,你这个class X没有public的默认构造函数是罪魁祸首。
lkcowboy 2002-12-12
  • 打赏
  • 举报
回复
auto_ptr最好不要用在stl容器中
shornmao 2002-12-09
  • 打赏
  • 举报
回复
你对vector的使用要求元素提供default ctor,编译器可以为Node<>合成一个,但是他合成出来的default ctor必须调用auto_ptr<>的default ctor,由于auto_ptr的实现定义了ctor,而它的语义又不允许定义default ctor(实际上是被放在了private区段),所以编译出错。
实际上auto_ptr在STL中几乎没有用武之地,因为他的复制语义和STL不一致,可以使用boost的shared_ptr或者自己实现一个类似的智能指针,但是提供值共享的复制语义,而不是像auto_ptr那样的拥有权转移的复制语义。
ThinkX 2002-12-06
  • 打赏
  • 举报
回复
不要将auto_ptr放在容器中,stl也建议不要这么做。
mmosquito 2002-12-05
  • 打赏
  • 举报
回复
i dont know why and it is ok under vc6
ddmpqcw 2002-12-05
  • 打赏
  • 举报
回复
auto_ptr最好不要用在stl容器中
r4 2002-12-05
  • 打赏
  • 举报
回复
你那个智能指针的声明根本就是错的吧,看看资料就有了
RedProgramer 2002-12-04
  • 打赏
  • 举报
回复
你的类必须要有自己的缺省的构造函数,并且还要重载一些运算函数
张友邦 2002-12-04
  • 打赏
  • 举报
回复
先弄清楚auto_ptr的原理,然后是怎么使用。

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧