矢量错误,无法让 Push_back 工作

发布于 2024-12-17 02:05:20 字数 1139 浏览 2 评论 0 原文

这只是未注释的代码的一个片段。打包向量不断在 push_back() 处导致错误,我不太确定为什么:

编辑:它已经更新说

vector<BinTreeNode<HuffmanToken<Pixel>* > > packing = new vector<BinTreeNode<HuffmanToken<Pixel> > >();

但是,仍然存在即使调整了模板,分配器也会出错。

没有匹配的函数来调用 std::vector 、 std::allocator > > :: Push_back(BinTreeNode > >&

BinTree<HuffmanToken<Pixel> >* Huffman::buildTree(const vector<HuffmanToken<Pixel>>& tokens) {

BinTreeNode<HuffmanToken<Pixel> >* g1 = new BinTreeNode<HuffmanToken<Pixel> >();
BinTreeNode<HuffmanToken<Pixel> >* g2 = new BinTreeNode<HuffmanToken<Pixel> >();
BinTreeNode<HuffmanToken<Pixel> >* g3 = new BinTreeNode<HuffmanToken<Pixel> >();

vector<HuffmanToken<Pixel> > packing ;

vector<HuffmanToken<Pixel> >::const_iterator it;

it = tokens.begin();

for(int i = 0; i < tokens.size(); i++) {
  g1 -> setValue(tokens.at(i));
  packing.push_back(g1);
}

This is just a snippet of the uncommented code. The packing vector keeps causing an error at the push_back(), and I'm not quite sure why:

EDIT: It has been updated to say

vector<BinTreeNode<HuffmanToken<Pixel>* > > packing = new vector<BinTreeNode<HuffmanToken<Pixel> > >();

however, there is still the allocator error even with the adjusted templates.

no matching function to call std::vector , std::allocator > > :: push_back(BinTreeNode > >&

BinTree<HuffmanToken<Pixel> >* Huffman::buildTree(const vector<HuffmanToken<Pixel>>& tokens) {

BinTreeNode<HuffmanToken<Pixel> >* g1 = new BinTreeNode<HuffmanToken<Pixel> >();
BinTreeNode<HuffmanToken<Pixel> >* g2 = new BinTreeNode<HuffmanToken<Pixel> >();
BinTreeNode<HuffmanToken<Pixel> >* g3 = new BinTreeNode<HuffmanToken<Pixel> >();

vector<HuffmanToken<Pixel> > packing ;

vector<HuffmanToken<Pixel> >::const_iterator it;

it = tokens.begin();

for(int i = 0; i < tokens.size(); i++) {
  g1 -> setValue(tokens.at(i));
  packing.push_back(g1);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

好倦 2024-12-24 02:05:20

您的向量需要 HuffmanToken 对象,但您尝试 push_back 一个 BinTreeNode 对象>* 指针。只需确保您的向量具有正确的模板类型即可。

编辑

考虑到您的更新,我决定放弃所有应有的代码:

BinTree<HuffmanToken<Pixel> >* Huffman::buildTree(const vector<HuffmanToken<Pixel>>& tokens) {

    BinTreeNode<HuffmanToken<Pixel> >* g1 = new BinTreeNode<HuffmanToken<Pixel> >();
    BinTreeNode<HuffmanToken<Pixel> >* g2 = new BinTreeNode<HuffmanToken<Pixel> >();
    BinTreeNode<HuffmanToken<Pixel> >* g3 = new BinTreeNode<HuffmanToken<Pixel> >();

    vector<BinTreeNode<HuffmanToken<Pixel> >*> packing ;

    vector<BinTreeNode<HuffmanToken<Pixel> >*>::const_iterator it;

    it = tokens.begin();

    for(int i = 0; i < tokens.size(); i++) {
        g1 -> setValue(tokens.at(i));
        packing.push_back(g1);
    }

与原始代码的唯一区别是 vector 。 > 替换为 vector >*>(适用于向量本身以及迭代器)。

Your vector is expecting HuffmanToken<Pixel> objects, but you're trying to push_back a BinTreeNode<HuffmanToken<Pixel> >* pointer. Just make sure your vector has the right template type.

Edit

Considering your update, I decided to throw up all the code as it should be:

BinTree<HuffmanToken<Pixel> >* Huffman::buildTree(const vector<HuffmanToken<Pixel>>& tokens) {

    BinTreeNode<HuffmanToken<Pixel> >* g1 = new BinTreeNode<HuffmanToken<Pixel> >();
    BinTreeNode<HuffmanToken<Pixel> >* g2 = new BinTreeNode<HuffmanToken<Pixel> >();
    BinTreeNode<HuffmanToken<Pixel> >* g3 = new BinTreeNode<HuffmanToken<Pixel> >();

    vector<BinTreeNode<HuffmanToken<Pixel> >*> packing ;

    vector<BinTreeNode<HuffmanToken<Pixel> >*>::const_iterator it;

    it = tokens.begin();

    for(int i = 0; i < tokens.size(); i++) {
        g1 -> setValue(tokens.at(i));
        packing.push_back(g1);
    }

The only difference from the original code is that vector<HuffmanToken<Pixel> > is replaced with vector<BinTreeNode<HuffmanToken<Pixel> >*> (that goes for the vector itself, as well as the iterator).

宫墨修音 2024-12-24 02:05:20

你的类型不匹配。您有一个由 HuffmanToken 组成的向量,并且您正在尝试将 BinTreeNode 推送到> * 到它上面。

Your types don't match. You have a vector of HuffmanToken<Pixel>s and you're trying to push a BinTreeNode<HuffmanToken<Pixel> > * onto it.

冷弦 2024-12-24 02:05:20
BinTreeNode<HuffmanToken<Pixel> >* g1 = new BinTreeNode<HuffmanToken<Pixel> >();

g1 的类型为 BinTreeNode >* 即,它是一个指针类型。但 packing 的类型为 vector 。 >。向量保存的是对象,而不是指向对象的指针。

BinTreeNode<HuffmanToken<Pixel> >* g1 = new BinTreeNode<HuffmanToken<Pixel> >();

The type of g1 is BinTreeNode<HuffmanToken<Pixel> >* i.e., it is a pointer type. But packing is of type vector<HuffmanToken<Pixel> >. What the vector holds is objects but not pointers to objects.

梦中楼上月下 2024-12-24 02:05:20

您的向量的类型为 HuffmanToken 但您正在尝试推送类型
BinTreeNode >*
进入其中。

Your vector is of type HuffmanToken<Pixel> but you are trying to push type
BinTreeNode<HuffmanToken<Pixel> >*
into it.

诗笺 2024-12-24 02:05:20

这里的问题是您正在创建一个应该保存 HuffmanToken 类型的项目的向量。您尝试将 BinTreeNode 推送到向量中,而不是将该类型的项目推送到向量中。 >*

这是行不通的。

您可能想要推送的是 g1->getValue() 的返回值(如果有这样的方法的话......)。

The problem here is that you are creating a vector that is supposed to hold items of type HuffmanToken<Pixel>. Instead of pushing items of that type into the vector, you try to push in a BinTreeNode<HuffmanToken<Pixel> >*.

And this cannot work.

What you probably wanted to push was the return value of g1->getValue() (if there is such a method at all...).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文