C++ “XCode错误”;架构x86“U 64”的未定义符号;对于C++;
我一直在尝试编写一个气泡排序程序,其中用户输入数组大小,然后计算机为所有数组值生成随机数。之后,程序询问用户是否希望以升序或降序方式对数组进行排序。在那之后,它通过气泡排序运行。我得到一个错误: 架构x86_64的未定义符号:“ArraySorter(int,int)”, 引用自: _main in main.o ld:未找到架构x86_64的符号clang:错误:链接器命令失败,退出代码为1(使用-v查看 调用) 我知道这是一个链接器错误,但我不知道是什么。谁能解释一下我的错误以及如何纠正它? 代码如下:C++ “XCode错误”;架构x86“U 64”的未定义符号;对于C++;,c++,xcode,debugging,function-pointers,bubble-sort,C++,Xcode,Debugging,Function Pointers,Bubble Sort,我一直在尝试编写一个气泡排序程序,其中用户输入数组大小,然后计算机为所有数组值生成随机数。之后,程序询问用户是否希望以升序或降序方式对数组进行排序。在那之后,它通过气泡排序运行。我得到一个错误: 架构x86_64的未定义符号:“ArraySorter(int,int)”, 引用自: _main in main.o ld:未找到架构x86_64的符号clang:错误:链接器命令失败,退出代码为1(使用-v查看 调用) 我知道这是一个链接器错误,但我不知道是什么。谁能解释一下我的错误以及如何纠正它?
#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's 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
}
#包括
#包括//for time()
#包括//用于srand()和rand()
#包括//for swap()
使用名称空间std;
类型定义布尔(*pfPointer)(整数,整数);
void数组排序器(int,int,pfPointer);
布尔升序(int,int);
布尔下降(int,int);
int main(){
cout>nArraySize;
int*pnArray=newint[nArraySize];
静态施法(时间(0));
库特>努瑟斯特;
cout数组排序器的函数原型采用(int,int,pfPointer)
,但函数定义采用(int[],int,pfPointer)
将ArraySorter
的原型更改为take(int[],int,pfPointer)
,并将对ArraySorter
的调用更改为ArraySorter(pnArray,nArraySize,Descending)
而不是ArraySorter(pnArray[nArraySize],nArraySize,Descending)
非常感谢。我知道函数原型是错误的(我做了您概述的更改,代码工作正常),但我不明白你为什么不在函数调用中指定数组大小。这是因为它是动态分配的吗?只是想知道@nickthomphsony你在函数调用中指定了数组大小!我引用了这个建议:“将对ArraySorter的调用更改为ArraySorter(pnArray,nArraySize,Descending)而不是ArraySorter(pnArray[nArraySize],nArraySize,Descending)。“首先,pnArray[nArraySize]
是一个语法错误;它试图访问数组末尾的元素,可能会出错。但即使nArraySize
是pnArray
的有效索引,也会传递pnArray[nArraySize]
将是int
类型,而不是int*
或int[]
。非常感谢,我现在理解了。我感谢您的帮助。