A
Aaron Broad
I can't get STL sort to work for this little huffman encoding program
i'm writing. I get a whole whack of errors eminating from the line
that call sthe sort. They all look like the following error:
main.cpp:44: instantiated from here
/usr/include/c++/3.2.2/bits/stl_algo.h:2004: no match for `
std::_List_iterator<HuffmanNode, HuffmanNode&, HuffmanNode*>& +
int'
operator
/usr/include/c++/3.2.2/bits/stl_algo.h:2079: instantiated from `void
std::__final_insertion_sort(_RandomAccessIter, _RandomAccessIter,
_Compare) [with _RandomAccessIter = std::_List_iterator<HuffmanNode,
HuffmanNode&, HuffmanNode*>, _Compare = bool (*)(const HuffmanNode&,
const HuffmanNode&)]'
/usr/include/c++/3.2.2/bits/stl_algo.h:2210: instantiated from `void
std::sort(_RandomAccessIter, _RandomAccessIter, _Compare) [with
_RandomAccessIter = std::_List_iterator<HuffmanNode, HuffmanNode&,
HuffmanNode*>, _Compare = bool (*)(const HuffmanNode&, const
HuffmanNode&)]'
main.cpp:44: instantiated from here
/usr/include/c++/3.2.2/bits/stl_algo.h:2008: no match for `
std::_List_iterator<HuffmanNode, HuffmanNode&, HuffmanNode*>& +
int'
operator
here is my code:
#include "HuffmanNode.h"
bool huffmanSortCriterion(const HuffmanNode& h1, const HuffmanNode&
h2);
/*main.cpp*/
int main(int argc, char *argv[]) {
//validate arguments
//create array
char frequency[256];
for(int i = 0; i < 256; ++i)
frequency = 0;
//open file for input
std::fstream input_file("test.txt", std::ios::in |
std::ios::binary);
int total = 0;
while(!input_file.eof()) {
++frequency[input_file.get()];
++total;
}
for(int i = 0; i < 256; ++i)
std::cout << (int)frequency;
std::list<HuffmanNode> forest;
float ftotal = (float)total;
for(int i = 0; i < 256; ++i)
if(frequency != 0)
forest.push_back(HuffmanNode(frequency/ftotal, (char)i));
std::cout << forest.size();
std::sort(forest.begin(), forest.end(), huffmanSortCriterion);
/* ... */
}
bool huffmanSortCriterion(const HuffmanNode& h1, const HuffmanNode&
h2) {
return h1.getWeight() < h2.getWeight();
}
/*HuffmanNode.h*/
#ifndef HuffmanNode_H
#define HuffmanNode_H
class HuffmanNode {
private:
float weight;
char character;
HuffmanNode *zeroChild;
HuffmanNode *oneChild;
public:
HuffmanNode() {}
HuffmanNode(const float Weight, const char Character);
HuffmanNode(HuffmanNode *ZeroChild, HuffmanNode *OneChild);
inline float getWeight() const;
inline char getChar();
};
#endif
/*HuffmanNode.cpp*/
#include "HuffmanNode.h"
HuffmanNode::HuffmanNode(const float Weight, const char Character) {
weight = Weight;
character = Character;
zeroChild = 0;
oneChild = 0;
}
HuffmanNode::HuffmanNode(HuffmanNode *ZeroChild, HuffmanNode
*OneChild) {
zeroChild = ZeroChild;
oneChild = OneChild;
weight = zeroChild->getWeight() + oneChild->getWeight();
}
inline float HuffmanNode::getWeight() const {
return (weight);
}
inline char HuffmanNode::getChar() {
return (character);
}
/* end of code */
Thanks in advance,
Aaron Broad
i'm writing. I get a whole whack of errors eminating from the line
that call sthe sort. They all look like the following error:
main.cpp:44: instantiated from here
/usr/include/c++/3.2.2/bits/stl_algo.h:2004: no match for `
std::_List_iterator<HuffmanNode, HuffmanNode&, HuffmanNode*>& +
int'
operator
/usr/include/c++/3.2.2/bits/stl_algo.h:2079: instantiated from `void
std::__final_insertion_sort(_RandomAccessIter, _RandomAccessIter,
_Compare) [with _RandomAccessIter = std::_List_iterator<HuffmanNode,
HuffmanNode&, HuffmanNode*>, _Compare = bool (*)(const HuffmanNode&,
const HuffmanNode&)]'
/usr/include/c++/3.2.2/bits/stl_algo.h:2210: instantiated from `void
std::sort(_RandomAccessIter, _RandomAccessIter, _Compare) [with
_RandomAccessIter = std::_List_iterator<HuffmanNode, HuffmanNode&,
HuffmanNode*>, _Compare = bool (*)(const HuffmanNode&, const
HuffmanNode&)]'
main.cpp:44: instantiated from here
/usr/include/c++/3.2.2/bits/stl_algo.h:2008: no match for `
std::_List_iterator<HuffmanNode, HuffmanNode&, HuffmanNode*>& +
int'
operator
here is my code:
#include "HuffmanNode.h"
bool huffmanSortCriterion(const HuffmanNode& h1, const HuffmanNode&
h2);
/*main.cpp*/
int main(int argc, char *argv[]) {
//validate arguments
//create array
char frequency[256];
for(int i = 0; i < 256; ++i)
frequency = 0;
//open file for input
std::fstream input_file("test.txt", std::ios::in |
std::ios::binary);
int total = 0;
while(!input_file.eof()) {
++frequency[input_file.get()];
++total;
}
for(int i = 0; i < 256; ++i)
std::cout << (int)frequency;
std::list<HuffmanNode> forest;
float ftotal = (float)total;
for(int i = 0; i < 256; ++i)
if(frequency != 0)
forest.push_back(HuffmanNode(frequency/ftotal, (char)i));
std::cout << forest.size();
std::sort(forest.begin(), forest.end(), huffmanSortCriterion);
/* ... */
}
bool huffmanSortCriterion(const HuffmanNode& h1, const HuffmanNode&
h2) {
return h1.getWeight() < h2.getWeight();
}
/*HuffmanNode.h*/
#ifndef HuffmanNode_H
#define HuffmanNode_H
class HuffmanNode {
private:
float weight;
char character;
HuffmanNode *zeroChild;
HuffmanNode *oneChild;
public:
HuffmanNode() {}
HuffmanNode(const float Weight, const char Character);
HuffmanNode(HuffmanNode *ZeroChild, HuffmanNode *OneChild);
inline float getWeight() const;
inline char getChar();
};
#endif
/*HuffmanNode.cpp*/
#include "HuffmanNode.h"
HuffmanNode::HuffmanNode(const float Weight, const char Character) {
weight = Weight;
character = Character;
zeroChild = 0;
oneChild = 0;
}
HuffmanNode::HuffmanNode(HuffmanNode *ZeroChild, HuffmanNode
*OneChild) {
zeroChild = ZeroChild;
oneChild = OneChild;
weight = zeroChild->getWeight() + oneChild->getWeight();
}
inline float HuffmanNode::getWeight() const {
return (weight);
}
inline char HuffmanNode::getChar() {
return (character);
}
/* end of code */
Thanks in advance,
Aaron Broad