LNK 2019和LNK1120在简单的c ++排序程序上出错

时间:2012-08-01 09:05:13

标签: c++ sorting linker

我应该这样说,我是一名本科生的CS学生,几乎不知道编译器是如何工作的。对我来说,这些错误看起来像是完全的胡言乱语,在网上搜了大约半个小时之后,我恐怕我已经辞职了,寻求帮助=(

这是我收到的错误:

  

Prog4Main.obj:错误LNK2019:未解析的外部符号“void __cdecl shellSort(struct SortData * const,int)”(?shellSort @@ YAXQAUSortData @@ H @ Z)在函数_main中引用

     

* \ Debug \ prog4 again.exe:致命错误LNK1120:1个未解析的外部

这是我遇到的唯一两个错误。

所以,关于我的程序试图完成的事情:

这是我的c ++课程中的家庭作业。我们必须实现在课堂上向我们展示的5种排序算法。我已经实现了冒泡排序和插入排序就好了,但是shell排序给了我这些错误。它们在同一个.cpp文件中,当我注释掉shellSort()函数调用时,我没有编译错误。

由于它已经到了我夏季学期c ++课程的末尾,而且我们没有足够的时间深入介绍排序算法,教师几乎给了我们这个程序的所有代码。我们只需要更改变量名称并调整排序函数的形式以满足我们的需求。因为这个,我知道我的所有代码都是正确的,因为我没有写它。

这是在VS2010(不是快递,我认为它的终极版),我的应用程序是一个win32控制台应用程序,我检查并确保user32.lib是链接的(因为它在其他链接错误之一中提到过)帖子)。我真的不知道如何解决这个问题....

见下问题代码:

//---------------------- 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 -----------------------------

非常感谢你们给予的任何帮助。

编辑:继承原型和功能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; } }

1 个答案:

答案 0 :(得分:5)

C ++区分大小写:shellSort vs ShellSort。你已经声明并调用了一个函数,但实现了一个完全不相关的函数!

这是链接器错误而不是编译器错误的原因是前向声明告诉编译器“将在某处定义此函数shellSort”,但不指定位置。因为它不在这个.cpp文件中,所以编译器会接受它的说法,它必须在一个不同的.cpp文件中,以后将与该.cpp文件链接在一起。当你进入链接阶段时,仍然没有函数shellSort

相关问题