I should preface this by saying that I am an undergrad CS student with little to no knowledge of how compilers work. To me these errors look like complete gibberish, and after scouring the web for about half an hour, I’m afraid I’ve resigned myself to asking for help =(
here are the errors I’m receiving:
Prog4Main.obj : error LNK2019: unresolved external symbol “void __cdecl shellSort(struct SortData * const,int)” (?shellSort@@YAXQAUSortData@@H@Z) referenced in function _main
*\Debug\prog4 again.exe : fatal error LNK1120: 1 unresolved externals
These are the only two errors I’m getting.
So, onto what my program is trying to accomplish:
This is a homework assignment in my c++ class. We have to implement 5 of the sorting algorithms shown to us in class. I have implemented bubble sort and insertion sort just fine, but shell sort is giving me these errors. they are in the same .cpp file, and i have no compile errors when i comment out the shellSort() function call.
As it is reaching the end of my summer semester c++ class and we didn’t have enough time to cover sort algorithms in depth, the instructor pretty much gave us all code for this program. we just have to change variable names and tweak the form of the sort functions to suit our needs. because of this, I know all my code is correct, as I didn’t write it.
This is in VS2010 (not express, I think its ultimate), my application is a win32 console application, and I’ve checked and insured that user32.lib is linked (as it was mentioned in one of the other link error posts). I seriously have no idea how to resolve this problem….
heres the problem code:
//---------------------- Begin Shell Sort -----------------------
totalTime = 0.0; // Initialize the time variable
sortError = false; // Initialize the error flag
// For each of 5 runs on this algorithm
for(int i=0; i<5; i++)
{
// Read the data. You must do this before each sort
if(!ReadData(DataArray, NUMRECS))
{
cout << "Failed to read data file.\n Program Terminated\n";
return 0;
}
// Start the microsecond timer
mst->Start();
shellSort(DataArray, NUMRECS);// Call the sort function here PROBLEM SPOT
// Stop the microsecond timer
mst->Stop();
// Check for error in sorting
if(!CheckData(DataArray, NUMRECS))
sortError = true;
else
totalTime += mst->getTime(); // Add to the total time for this sort
}
// Calculate the average time
averageTime = totalTime / 5.0;
// Output the results after checking to be sure the sort worked
if(sortError)
cout << "Error in Shell Sort\n\n";
else
cout << "Shell sort took " << averageTime << " seconds to complete\n\n";
//------------------------ End Shell Sort -----------------------------
Any help you guys can give is much appreciated.
EDIT: heres the prototype and function def…
#define NUMRECS 10000
void shellSort(SortData DataArray[NUMRECS], int count);
int main{...}
//---------------------SHELL SORT------------------------
void ShellSort(SortData DataArray[], int count)
{
int i, delta;
delta = count;
do
{
delta = 1 + delta / 3;
for(i=0; i<delta; i++)
DeltaInsertionSort(DataArray, i, delta, count);
}
while(delta > 1);
}
void DeltaInsertionSort(SortData DataArray[], int I, int Delta, int count)
{
int j, k;
int key;
int NotDone;
SortData temp;
j = I + Delta;
while(j < count)
{
key = DataArray[j].key; /* Get next key to sort */
temp = DataArray[j]; /* Remove and hold */
/* Do insertion sort on this key in the block of delta records */
/* Move each struct where DataArray[j].key > key to the right */
/* by delta spaces and insert the key there. */
insertion(DataArray, key);
k = j;
NotDone = TRUE;
do
{
if(DataArray[k - Delta].key <= key)
NotDone = FALSE; /* Terminate the loop */
else
{
DataArray[k] = DataArray[k - Delta];
k -= Delta;
if(k==I) NotDone = FALSE;
}
}
while(NotDone);
/* Insert the moved key */
DataArray[k] = temp;
/* Get next key to insert--one full delta increment to the right */
j += Delta;
}
}
C++ is case-sensitive:
shellSortvsShellSort. You’ve declared and called one function, but implemented a completely unrelated function!The reason this is a linker error rather than a compiler error, is that the forward declaration tells the compiler “there’s going to be a definition of this function
shellSortsomewhere”, but doesn’t specify where. Since it’s not in this .cpp file, the compiler takes your word for it that it must be in a different .cpp file that will be linked together with this .cpp file later. When you get to the linking stage, there’s still no functionshellSort.