函数不会接受auto_ptr的迭代器

时间:2012-10-01 13:58:43

标签: c++ standard-library auto-ptr huffman-code

我写了一些我试图解决的有缺陷的Huff压缩代码。我做的第一件事是将指针切换到auto_ptr(有理由我没有使用另一个智能指针)。我创建了auto_ptr的向量,但是当我尝试通过*(vector.begin())将auto_ptr传递给函数时,它不起作用。

我的功能代码我正在尝试将所有权传递给(它是一个成员函数,如set_node):

struct Node {
    int weight;
    char litteral;
    auto_ptr<Node> childL;
    auto_ptr<Node> childR;
    void set_node(int w, char l, auto_ptr<Node>& L(), auto_ptr<Node>& R()){
        weight = w;
        litteral = l;
        childL = L;
        childR = R;
    }
};

这就是我尝试调用它的方式(p是节点):

p.set_node(w, '*', *nodes->begin(), *(nodes->begin()+1));

这是向量的声明方式:

vector<auto_ptr<Node> >* nodes = new vector<auto_ptr<Node> >;

1 个答案:

答案 0 :(得分:6)

无法std::auto_ptr中使用std::vector。你需要找到一个替代方案。问题是std::auto_ptr中没有副本。复制构造函数在某种意义上是一个 move 操作,它从原始自动指针中窃取内容并将其移动到新的自动指针。该操作要求源是非const std::auto_ptr(因为它从中删除了托管对象)。