Multiple defintion

I'm working on my first multi file project and I'm getting multiple definition error on all my methods in one of my source files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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


Here's the complete source file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "HuffTree.h"
#include "PQueue.h"

HuffTree::~HuffTree(){

}
void HuffTree::buildTree(char * chs, int * freqs, int size){
  PQueue <HuffNode, 100>que;
  for( int i = 0; i < size; i++ ){
    que.insert( new HuffNode( 0, 0, chs[i], freqs[i] ) );
  }


  while( que.size() > 1 ){
    HuffNode  firstSmallest = que.findMin();
    que.deleteMin();
    firstSmallest.code = "0" + firstSmallest.code;
    HuffNode  secondSmallest = que.findMin();
    que.deleteMin();
    secondSmallest.code = "1" + secondSmallest.code;

    que.insert( new HuffNode( &firstSmallest, &secondSmallest, firstSmallest.freq + secondSmallest.freq, '\0' ) );
  }
  HuffNode end = que.findMin();
  _root = &end;
}
bool operator<(const HuffNode &c1, const HuffNode &c2){
      return c1.freq < c2.freq;
}
string HuffTree::getCode(char c){
  return "you need to fix this";
}


For more background:

PQueue.h includes PQueue.cpp at the end
HuffTree.cpp includes HuffTree.h and PQueue.h
I'd have to see the .h files. Do you have #pragma once at the top of them? Are any of them including a cpp file?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef PRIORITY_QUEUE_H
#define PRIORITY_QUEUE_H

#include <iostream>
using namespace 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 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef HUFFTREE_H
#define HUFFTREE_H
//#include "PQueue.h"

#include <string>
using namespace 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
    unsigned int 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


    friend bool operator<(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

Last edited on
Could you make all the code available as a zip file?
closed account (1vRz3TCk)
You should not include a cpp file in a header file: #include "PQueue.cpp"

also don't using namespace ... in header files.
@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.

closed account (1vRz3TCk)
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...

makefile issue?
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.