4

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);
}
user357051
2,3584 gold badges24 silver badges44 bronze badges
asked Nov 21, 2011 at 18:26

5 Answers 5

7

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

answered Nov 21, 2011 at 18:30
Sign up to request clarification or add additional context in comments.

2 Comments

I'm still getting the following error "request for member 'push_back' in 'packing', which is of non-class type 'std::vector<BinTreeNode<HuffmanToken<Pixel> >, std::allocator<BinTreeNode<HuffmanToken<Pixel> > > >*" with this change to the vector vector<BinTreeNode<HuffmanToken<Pixel> > >* packing = new vector<BinTreeNode<HuffmanToken<Pixel> > >();
Your vector shouldn't need to be a pointer. It should just store pointers. If I understand your code correctly, your vector should be declared as vector<BinTreeNode<HuffmanToken<Pixel>* > > packing;.
1

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.

answered Nov 21, 2011 at 18:30

Comments

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

answered Nov 21, 2011 at 18:31

1 Comment

*g1 is still not of type HuffmanToken<Pixel> but rather of type BinTreeNode<HuffmanToken<Pixel> >.
1

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

answered Nov 21, 2011 at 18:32

Comments

0

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

answered Nov 21, 2011 at 18:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.