c++ - C++ 的 XCode 错误 "Undefined symbols for architecture x86_64"

标签 c++ xcode debugging function-pointers bubble-sort

我一直在尝试编写一个冒泡排序程序,用户在其中输入数组大小,然后计算机为数组的所有值生成随机数。之后,程序询问用户是否希望数组以升序或降序方式排序。之后,它贯穿冒泡排序。我收到错误:

Undefined symbols for architecture x86_64: "ArraySorter(int, int)", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)

我知道这是一个链接器错误,但我不知道是什么。谁能解释我的错误以及如何解决? 这是代码:

#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
}

附言我正在运行 XCode 6.1.1

最佳答案

ArraySorter 的函数原型(prototype)采用(int, int, pfPointer),但函数定义采用(int [], int, pfPointer)

ArraySorter 的原型(prototype)更改为采用 (int [], int, pfPointer),并将对 ArraySorter 的调用更改为 ArraySorter(pnArray, nArraySize, Descending) 而不是 ArraySorter(pnArray[nArraySize], nArraySize, Descending)

关于c++ - C++ 的 XCode 错误 "Undefined symbols for architecture x86_64",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27761144/

相关文章:

c++11 类内成员初始化

iphone - 在 UITableView 中显示多种行类型

javascript - 从 UIWebView 添加 javascript 到页面中

PHP - error_log() 不会打印到文件

hibernate - 如何从 JBoss 7 中的 Hibernate 获取 JDBC 绑定(bind)参数?

c++ - C++ 中的 Clang vector 扩展和相等运算符

C++ 类双关语类型

c++ - 这里需要内存栅栏吗?

ios - 为什么我可以通过XCode在设备上运行我的应用程序,但不能对其进行配置文件?

excel - 通过循环调试