XCode Error "Undefined symbols for x86_64 architecture" for C ++
I am trying to code a bubble sorting program where the user enters the size of an array and then the computer generates random numbers for all values in the array. The program then asks the user if they want to sort the array in ascending or descending order. After that, it goes through a bubble sort. I am getting the error:
Undefined symbols for x86_64 architecture: "ArraySorter (int, int)", ref: _main in main.o ld: symbol not found for x86_64 architecture clang: error: linker command ended with exit code 1 (use -v to see the Call)
I know this is a linker bug, but I can't figure out what. Can anyone explain my error and how to fix it? Here is the code:
#include <iostream>
#include <ctime> // for time()
#include <cstdlib> // for srand() and rand()
#include <algorithm>// for swap()
using namespace std;
typedef bool (*pfPointer) (int, int);
void ArraySorter(int, int, pfPointer);
bool Ascending(int, int);
bool Descending(int, int);
int main() {
cout << "Enter the size of the array: ";
int nArraySize;
cin >> nArraySize;
int *pnArray = new int [nArraySize];
srand(static_cast<int>(time(0)));
cout << "Do you want your array to sort in ascending or descending order (a/d)? ";
char nUserSort;
cin >> nUserSort;
cout << '\n';
if (nUserSort == 'a')
ArraySorter(pnArray[nArraySize], nArraySize, Descending); //This allows the user to decide whether they want an ascending or descending function
else
ArraySorter(pnArray[nArraySize], nArraySize, Ascending);
//This prints out the array
for(int qqq = 0; qqq < nArraySize - 1; qqq++)
{
cout << pnArray[qqq] << " || ";
}
cout << pnArray[nArraySize - 1] << endl;
delete [] pnArray;
return 0;
}
void ArraySorter(int pnArray[], int nArraySize, pfPointer pComparison)
{
//Assigns a random number to each value in the array pnArray
for (int iii = 0; iii< nArraySize; iii++)
{
pnArray[iii] = rand();
}
// Will change the starting index of the array each time the loop runs through
for (int nStartIndex = 0; nStartIndex < nArraySize; nStartIndex++)
{
// Declares a variable to find the location of the smallest variable in the array
int nSmallestIndex = nStartIndex;
// Runs through each array value and finds the smallest one, then registers it
for (int nCurrentIndex = nSmallestIndex +1; nCurrentIndex < nArraySize; nCurrentIndex++)
{
// If the current array value is less than the starting array value, it will log it location in the array in nSmallestIndex
if (pComparison(pnArray[nCurrentIndex] , pnArray[nSmallestIndex]))
{
nSmallestIndex = nCurrentIndex;
}
}
//Switches the smallest value with the starting value
swap(pnArray[nStartIndex], pnArray [nSmallestIndex]);
}
}
bool Ascending(int nValue1, int nValue2)
{
return nValue1 < nValue2; //Will sort the variables from smallest to greatest
}
bool Descending(int nValue1, int nValue2)
{
return nValue1 > nValue2; //Will sort the variables from greatest to smallest
}
PS I am running XCode 6.1.1
source to share
The function prototype ArraySorter
accepts (int, int, pfPointer)
, but the function definition accepts (int [], int, pfPointer)
.
Change the prototype ArraySorter
to take (int [], int, pfPointer)
, and change the calls to ArraySorter
to ArraySorter(pnArray, nArraySize, Descending)
, not to ArraySorter(pnArray[nArraySize], nArraySize, Descending)
.
source to share