2
votes

There's probably a 2 second solution to this but I've been trying to figure it out for the past hour and it's driving me crazy.

I'm trying to link together a .cpp and .h files into a main.cpp file, so it'd run there. But I get a multiple definition of every function inside my cpp file, which in causes "ld returned 1 exit status"

The more functions I add, the more errors I get.

Here's my code:

binaryHeap.h:

#ifndef Binary_H
#define Binary_H

#include <vector>

class BinaryHeap{
    private:
        std::vector <int> heap;
        int left(int parent);
        int right(int parent);
        void heapifyUp(int index);
        void heapifyDown(int index);
    public:
        BinaryHeap(); //Constructor
        void insert(int element); //inserts element into heap
        void deleteMin(); // deletes minimum element
        int extractMin();
        void displayHeap();
        int size();
};

#endif //Binary_H

binaryHeap.cpp:

#include <iostream>
#include "binaryHeap.h"

BinaryHeap::BinaryHeap(){
}

void BinaryHeap::heapifyUp(int index){
    int parent = (index - 1) / 2;

    if(index != 0 && parent >= 0 && heap[parent] > heap[index]){
        int temp = heap[index];
        heap[index] = heap[parent];
        heap[parent] = temp;
        heapifyUp(parent);
    }
}



void BinaryHeap::insert(int element){
    heap.push_back(element);
    heapifyUp(heap.size() - 1);
}



void BinaryHeap::displayHeap(){
    std::vector<int>::iterator pos = heap.begin();
    while (pos != heap.end()){
        std::cout << *pos << std::endl;
        pos++;
    }
}

main.cpp:

#include "binaryHeap.cpp"

int main(int argc, char* argv[]) {
    BinaryHeap h;
    h.insert(5);
    h.insert(2);
    h.insert(19);

    return 0;
}

And here's the errors:

main.o:main.cpp:(.text+0x0): multiple definition of BinaryHeap::BinaryHeap()' binaryHeap.o:binaryHeap.cpp:(.text+0x0): first defined here main.o:main.cpp:(.text+0x0): multiple definition of BinaryHeap::BinaryHeap()' binaryHeap.o:binaryHeap.cpp:(.text+0x0): first defined here main.o:main.cpp:(.text+0x20): multiple definition of BinaryHeap::heapifyUp(int)' binaryHeap.o:binaryHeap.cpp:(.text+0x20): first defined here main.o:main.cpp:(.text+0xfe): multiple definition of BinaryHeap::insert(int)' binaryHeap.o:binaryHeap.cpp:(.text+0xfe): first defined here main.o:main.cpp:(.text+0x13e): multiple definition of BinaryHeap::displayHeap()' binaryHeap.o:binaryHeap.cpp:(.text+0x13e): first defined here c:/program files (x86)/dev-cpp/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/4.8.1/../../../../x86_64-w64-mingw32/bin/ld.exe: binaryHeap.o: bad reloc address 0x0 in section.pdata$_ZnwyPv' collect2.exe: error: ld returned 1 exit status

C:\Users\~\Desktop\Coding Training\Binary Heap\Makefile.win:25: recipe for target '"Binary' failed mingw32-make.exe: *** ["Binary] Error 1

1
You must never include a .cpp file. In main.cpp, change the include to binaryHeap.h - CinCout

1 Answers

3
votes

Extending on my comment, you must never include a .cpp file in any other file. What #include does is copy all the contents from the included file into the place you are including it. This means that the contents of binaryHeap.cpp are compiled twice in your code. And this is what the errors are telling you.

To resolve errors in your case - in main.cpp, change the include to binaryHeap.h.

EDIT: Adding more explanation:

The include directive simply copies and pastes the source of the file to that spot. So, if you included binaryHeap.cpp in main.cpp, the source of binabyHeap is actually being compiled twice. And, once the linker starts combining your object files, it picks up multiple definitions of whatever you had in binaryHeap.cpp, and raises a few errors.