矢量错误,无法让 Push_back 工作
这只是未注释的代码的一个片段。打包向量不断在 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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您的向量需要
HuffmanToken
对象,但您尝试push_back
一个BinTreeNode 对象>*
指针。只需确保您的向量具有正确的模板类型即可。编辑
考虑到您的更新,我决定放弃所有应有的代码:
与原始代码的唯一区别是
vector 。 >
替换为vector >*>
(适用于向量
本身以及迭代器)。Your vector is expecting
HuffmanToken<Pixel>
objects, but you're trying topush_back
aBinTreeNode<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:
The only difference from the original code is that
vector<HuffmanToken<Pixel> >
is replaced withvector<BinTreeNode<HuffmanToken<Pixel> >*>
(that goes for thevector
itself, as well as the iterator).你的类型不匹配。您有一个由
HuffmanToken
组成的向量,并且您正在尝试将BinTreeNode 推送到> *
到它上面。Your types don't match. You have a vector of
HuffmanToken<Pixel>
s and you're trying to push aBinTreeNode<HuffmanToken<Pixel> > *
onto it.g1
的类型为BinTreeNode >*
即,它是一个指针类型。但packing
的类型为vector 。 >
。向量保存的是对象,而不是指向对象的指针。The type of
g1
isBinTreeNode<HuffmanToken<Pixel> >*
i.e., it is a pointer type. Butpacking
is of typevector<HuffmanToken<Pixel> >
. What the vector holds is objects but not pointers to objects.您的向量的类型为
HuffmanToken
但您正在尝试推送类型 >*
进入其中。BinTreeNode
Your vector is of type
HuffmanToken<Pixel>
but you are trying to push type
into it.BinTreeNode<HuffmanToken<Pixel> >*
这里的问题是您正在创建一个应该保存 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 aBinTreeNode<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...).