g++ -c HuffTree.cpp
[jkf1008@agate 8P]$ make testHuff1
g++ testHuff1.cpp HuffTree.o -o testHuff
HuffTree.o: In function `HuffTree::~HuffTree()':
HuffTree.cpp:(.text+0x0): multiple definition of `HuffTree::~HuffTree()'
/var/tmp/ccfAosRR.o:testHuff1.cpp:(.text+0x0): first defined here
HuffTree.o: In function `HuffTree::~HuffTree()':
HuffTree.cpp:(.text+0x0): multiple definition of `HuffTree::~HuffTree()'
/var/tmp/ccfAosRR.o:testHuff1.cpp:(.text+0x0): first defined here
HuffTree.o: In function `HuffTree::buildTree(char*, int*, int)':
HuffTree.cpp:(.text+0xc): multiple definition of `HuffTree::buildTree(char*, int*, int)'
/var/tmp/ccfAosRR.o:testHuff1.cpp:(.text+0xc): first defined here
HuffTree.o: In function `operator<(HuffNode const&, HuffNode const&)':
HuffTree.cpp:(.text+0x359): multiple definition of `operator<(HuffNode const&, HuffNode const&)'
/var/tmp/ccfAosRR.o:testHuff1.cpp:(.text+0x359): first defined here
HuffTree.o: In function `HuffTree::getCode[abi:cxx11](char)':
HuffTree.cpp:(.text+0x37a): multiple definition of `HuffTree::getCode[abi:cxx11](char)'
/var/tmp/ccfAosRR.o:testHuff1.cpp:(.text+0x37a): first defined here
collect2: error: ld returned 1 exit status
make: *** [makefile:12: testHuff1] Error 1
#ifndef PRIORITY_QUEUE_H
#define PRIORITY_QUEUE_H
#include <iostream>
usingnamespace std;
// Minimal Priority Queue implemented with a binary heap
// Stores item of type T
template< class T, int MAX_SIZE >
class PQueue{
public:
PQueue(); // default constructor, construct an empty heap
void insert(T); // insert an item; duplicates are allowed.
T findMin(); // return the smallest item from the queue
void deleteMin(); // remove the smallest item from the queue
bool isEmpty(); // test if the priority queue is logically empty
int size(); // return queue size
private:
int _size; // number of queue elements
T _array[MAX_SIZE+1]; // the heap array, items are stoed starting at index 1
void buildHeap(); // linear heap construction
void moveDown(int); // move down element at given index
void moveUp(); // move up the last element in the heap array
};
#include "PQueue.cpp"
#endif
#ifndef HUFFTREE_H
#define HUFFTREE_H
//#include "PQueue.h"
#include <string>
usingnamespace std;
struct HuffNode{
// default constructor
HuffNode(HuffNode* l = 0, HuffNode* r = 0, int f = 0, char d = '\0')
: left(l), right(r), freq(f), data(d){}
HuffNode * left, * right; // two child node
unsignedint freq; // freqency of the node
char data; // char value for leaf nodes; '\0' for internal nodes
string code; // Huffman code for leaf nodes; not assigned for internal nodes
friendbooloperator<(const HuffNode &c1, const HuffNode &c2);
};
class HuffTree{
public:
// get Huffman code and return it as a string
string getCode(char);
~HuffTree();
private:
HuffNode * _root; // root of the Huffman tree
};
#endif
@CodeMonkey, the cpp file he's including there has template definitions (it's basically a .tpp file). It looks like he's dealing with it correctly. The errors are about HuffTree, not PQueue.
You should not include a cpp file in a header file:
Actually since that .cpp file contains the implementations of the template class functions it is "acceptable" to #include that .cpp file at the end of the #include file. However it is usually recommend to name that file with a different extension, for example .inc instead of .cpp. The other alternative is to do away with the implementation file and just place everything in the header, which is usually the best way to handle the template code.
It looks like you have two source files implementing some of the same class functions, HuffTree.cpp and testHuff1.cpp.
Sorry it was more the fact that it is a .cpp than the content, I should have been clearer. I've never worked anywhere that allowed including .cpp file extensions, they tended to be .imp if they split implementations out. edit:It's 3:30am hear...should probably go to bed and get some sleep ;0)
PQueue.cpp looks like the only file that hasn't been shown...
We haven't seen testHuff1.cpp, either. I wanted to look at a zip of the whole thing (makefile, too) since it looks like some kind of organizational problem.