C++ 向量错误,无法让 push_back 工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8216605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:11:38  来源:igfitidea点击:

Vector error , cannot get push_back to work

c++stlvectorpush-back

提问by user1058359

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:

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

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 > >&

没有匹配的函数来调用 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);
}

回答by Ken Wayne VanderLinde

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

您的向量正在HuffmanToken<Pixel>等待对象,但您正在尝试指向push_back一个BinTreeNode<HuffmanToken<Pixel> >*指针。只需确保您的矢量具有正确的模板类型。

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 vectoritself, as well as the iterator).

与原始代码的唯一区别是vector<HuffmanToken<Pixel> >被替换为vector<BinTreeNode<HuffmanToken<Pixel> >*>(这适用于vector本身,以及迭代器)。

回答by David Schwartz

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.

你的类型不匹配。你有一个HuffmanToken<Pixel>s向量,你正试图将 a 推BinTreeNode<HuffmanToken<Pixel> > *到它上面。

回答by Mahesh

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

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

的类型g1BinTreeNode<HuffmanToken<Pixel> >*即,它是一个指针类型。但packing属于vector<HuffmanToken<Pixel> >. vector 保存的是对象,而不是指向对象的指针。

回答by Victor Parmar

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

您的矢量是有类型的,HuffmanToken<Pixel>但您正试图将类型推BinTreeNode<HuffmanToken<Pixel> >*入其中。

回答by cli_hlt

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> >*.

这里的问题是您正在创建一个应该保存 type 项的向量HuffmanToken<Pixel>。您尝试将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...).

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