error LNK 2005... already defined....Please help

I'm getting this error...
1>drive.obj : error LNK2005: "struct info h" (?h@@3Uinfo@@A) already defined in BubbleSort.obj
1>HeapSort.obj : error LNK2005: "struct info f" (?f@@3Uinfo@@A) already defined in drive.obj
1>InsertionSort.obj : error LNK2005: "struct info e" (?e@@3Uinfo@@A) already defined in drive.obj
1>MergeSort.obj : error LNK2005: "struct info g" (?g@@3Uinfo@@A) already defined in drive.obj
1>QuickSort.obj : error LNK2005: "struct info d" (?d@@3Uinfo@@A) already defined in drive.obj
1>SelectionSort.obj : error LNK2005: "struct info c" (?c@@3Uinfo@@A) already defined in drive.obj
1>drive.obj : error LNK2001: unresolved external symbol "public: static int info::count" (?count@info@@2HA)
1>drive.obj : error LNK2001: unresolved external symbol "public: static int info::counter" (?counter@info@@2HA)
\Project-5.exe : fatal error LNK1120: 2 unresolved externals.


And I believe this is the cleanest it has looked after the twelve hours I spent today... i have diff. files and in this post i'll separate them with ****...
P.S I don't think the problem is the sort alg because I already used them with a different drive.cpp and worked fine...Although I added a few things for this drive file I should mention that i'm trying to test the performance of this ALGorithms by calculating how many times they use swap and compare elements... I also started getting this error using the "show()" but i may be wrong. so here they go:
*****************performance.h
#ifndef PERFORMANCE_H
#define PERFORMANCE_H

#include <iostream>

using namespace std;

struct info{
static int count;
static int counter;

void comprCount(){

count = 0;
count += 1;
}

void swpCountr(){

counter = 0;
counter += 1;
}

};

#endif

**********************BubbleSort.cpp
#include "performance.h"
info h;


template<class ItemType>
void BubbleUp (ItemType values[], int startIndex, int endIndex, bool& sorted){
sorted = true;
for (int index = endIndex; index > startIndex; index--)

if (values[index] < values[index-1])
{
Swap(values[index], values[index-1]);
h.swpCountr();
sorted = false;
}
}

template<class ItemType>
void BubbleSort (ItemType values[], int numValues){
int current = 0;
bool sorted = false;
while (current < numValues - 1 && !sorted)
{
BubbleUp(values, current, numValues-1, sorted);
h.comprCount();
current++;
}
}

*********************SelectionSort.cpp
#include "performance.h"
info c;


template<class ItemType>
inline void Swap(ItemType& item1, ItemType& item2){

ItemType tempItem;

tempItem = item1;
item1 = item2;
item2 = tempItem;
}

template <class ItemType>
int MinIndex (ItemType values[], int startIndex, int endIndex){

int indexOfMin = startIndex;
for( int index = startIndex + 1; index <= endIndex; index++)

if (values[index] < values[indexOfMin])
{
indexOfMin = index;
c.comprCount();
}
return indexOfMin;
}

template<class ItemType>
void SelectionSort (ItemType values[], int numValues){

int endIndex = numValues-1;
for (int current = 0; current < endIndex; current++)
{
Swap(values[current], values[MinIndex(values, current, endIndex)]);
c.swpCountr();
}
}
****************InsertionSort.cpp
#include "performance.h"

info e;


template<class ItemType>
void InsertItem(ItemType values[], int startIndex, int endIndex){

bool finished = false;
int current = endIndex;
bool moreToSearch = (current != startIndex);

while (moreToSearch && !finished)
{ e.comprCount();
if (values[current] < values[current-1])
{
Swap(values[current], values[current-1]); e.swpCountr();
current--;
moreToSearch = (current != startIndex);
}
else
finished = true;
}
}

template<class ItemType>
void InsertionSort(ItemType values[], int numValues){

for ( int count = 0; count < numValues; count++)
InsertItem(values, 0, count);
}

*********************QuickSort.cpp
#include "performance.h"

info d;


template<class ItemType>
int Split(ItemType arr[], int first, int last)
{
int i = first, j = last;
int pivot = arr[(first + last) / 2];

while (i <= j)
{ d.comprCount();
while (arr[i] < pivot)
i++;

while (arr[j] > pivot)
j--;
if (i <= j)
{
Swap(arr[i], arr[j]); d.swpCountr();
i++;
j--;
}
}
return i;
}


template <class ItemType>
void QuickSort(ItemType arr[], int left, int right){

int index = Split(arr, left, right);
if (left < index - 1)
QuickSort(arr, left, index - 1);
if (index < right)
QuickSort(arr, index, right);
}

***************drive.cpp
#include "BubbleSort.cpp"
#include "InsertionSort.cpp"
#include "SelectionSort.cpp"
#include "QuickSort.cpp"
#include "MergeSort.cpp"
#include "HeapSort.cpp"
#include "performance.h"


using namespace std;

void show();

int main(){
int a [100];
int b [1000];

for (int i = 0; i < 100; i++)
a[i] = rand();
for (int i = 0; i < 1000; i++)
b[i] = rand();

BubbleSort(a, 100);
InsertionSort(a, 100);
SelectionSort(a, 100);
QuickSort(a, 0, 99);
MergeSort(a, 0, 99);
HeapSort(a, 100);

show();
}

void show(){
cout << "BubbleSort: '\n' comparisons: "<< h.count<< "\n Swaps: " << h.counter << endl;
cout << "InsertionSort: '\n' comparisons: "<< c.count << "\n Swaps: " << c.counter << endl;
cout << "SelectionSort: '\n' comparisons: "<< d.count << "\n Swaps: " << d.counter << endl;
cout << "QuickSort: '\n' comparisons: "<< e.count << "\n Swaps: " << e.counter << endl;
cout << "MergeSort: '\n' comparisons: "<< f.count << "\n Swaps: " << f.counter << endl;
cout << "HeapSort: '\n' comparisons: "<< g.count << "\n Swaps: " << 0 << endl;

}

I didn't include the last two sort Algorithms but it's the same idea. Plus like I mentioned before they work. The parts in Bold are the new things I added.
THank you very much any help would be greatly appreciated.

PS> one more thing i did another version of this program, but instead of performance.h i declared global variables and use the increment operator(++) instead of swpcounter() and comprcount(), but I had the same results. same error is what I mean.
cpp files aren't supposed to be #included.
Thanks hamsterman, but if I don't include the cpp files the main doesn't recognize the Sort Functions. or maybe you meant something else
1
2
3
4
5
6
#include "BubbleSort.cpp"
#include "InsertionSort.cpp"
#include "SelectionSort.cpp"
#include "QuickSort.cpp"
#include "MergeSort.cpp"
#include "HeapSort.cpp" 


You're not meant to #include cpp files.

You're meant to compile each one into a separate obj file, and link them together at link time.

Thanks hamsterman, but if I don't include the cpp files the main doesn't recognize the Sort Functions.


Each function should have a prototype, which would then usually be put into a header file, and that header file included in main.cpp - typically, you should not be including

1
2
3
4
5
6
#include "BubbleSort.cpp"
#include "InsertionSort.cpp"
#include "SelectionSort.cpp"
#include "QuickSort.cpp"
#include "MergeSort.cpp"
#include "HeapSort.cpp" 


but you should be including

1
2
3
4
5
6
#include "BubbleSort.h"
#include "InsertionSort.h"
#include "SelectionSort.h"
#include "QuickSort.h"
#include "MergeSort.h"
#include "HeapSort.h" 


where each of those header files contains the declarations of the objects, but not the definitions..

By including the cpp files, I expect you've got identical definitions of structs/classes in multiple object files, which is what the link errors are complaining about.
Last edited on
thanks Moschop... well I created the .h files and replaced the
1
2
3
4
5
6
#include "BubbleSort.cpp"
#include "InsertionSort.cpp"
#include "SelectionSort.cpp"
#include "QuickSort.cpp"
#include "MergeSort.cpp"
#include "HeapSort.cpp"  

for


1
2
3
4
5
6
#include "BubbleSort.h"
#include "InsertionSort.h"
#include "SelectionSort.h"
#include "QuickSort.h"
#include "MergeSort.h"
#include "HeapSort.h" 


in the main... but it didn't stop the problem.... when i try to compile i still get the same error plus six new ones.

1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>drive.obj : error LNK2005: "class info h" (?h@@3Vinfo@@A) already defined in BubbleSort.obj
1>HeapSort.obj : error LNK2005: "class info f" (?f@@3Vinfo@@A) already defined in drive.obj
1>InsertionSort.obj : error LNK2005: "class info e" (?e@@3Vinfo@@A) already defined in drive.obj
1>MergeSort.obj : error LNK2005: "class info g" (?g@@3Vinfo@@A) already defined in drive.obj
1>QuickSort.obj : error LNK2005: "class info d" (?d@@3Vinfo@@A) already defined in drive.obj
1>SelectionSort.obj : error LNK2005: "class info c" (?c@@3Vinfo@@A) already defined in drive.obj
1>drive.obj : error LNK2019: unresolved external symbol "void __cdecl HeapSort<int>(int * const,int)" (??$HeapSort@H@@YAXQAHH@Z) referenced in function _main
1>drive.obj : error LNK2019: unresolved external symbol "void __cdecl MergeSort<int>(int * const,int,int)" (??$MergeSort@H@@YAXQAHHH@Z) referenced in function _main
1>drive.obj : error LNK2019: unresolved external symbol "void __cdecl QuickSort<int>(int * const,int,int)" (??$QuickSort@H@@YAXQAHHH@Z) referenced in function _main
1>drive.obj : error LNK2019: unresolved external symbol "void __cdecl SelectionSort<int>(int * const,int)" (??$SelectionSort@H@@YAXQAHH@Z) referenced in function _main
1>drive.obj : error LNK2019: unresolved external symbol "void __cdecl InsertionSort<int>(int * const,int)" (??$InsertionSort@H@@YAXQAHH@Z) referenced in function _main
1>drive.obj : error LNK2019: unresolved external symbol "void __cdecl BubbleSort<int>(int * const,int)" (??$BubbleSort@H@@YAXQAHH@Z) referenced in function _main
"Unresolved external symbol" indicates that the linker cannot find a compiled object file that contains the definition it is looking for.

I'd guess that you are not compiling the following files:

1
2
3
4
5
6
BubbleSort.cpp
InsertionSort.cpp
SelectionSort.cpp
QuickSort.cpp
MergeSort.cpp
HeapSort.cpp


Each cpp file has to be compiled into an object file, and then the linker links each object file together into the final executable. One (or both) of these steps is not being done.

If you are using an IDE, I suspect you have to do something along the lines of "add to project" for each cpp file you wish to compile and link.
Topic archived. No new replies allowed.