2

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

我试图将所有权传递给我的函数代码(它是作为 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> >;
4

1 回答 1

6

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

于 2012-10-01T14:05:39.193 回答