BubbleSort错误LNK2019

时间:2015-08-14 02:34:56

标签: c++

这是我的cpp代码和我得到的错误

  

错误LNK2019:未解析的外部符号" void __cdecl selectionSort(int * const,int&,int&)" (?selectionSort @@ YAXQAHAAH1 @ Z)在函数_main

中引用
#include <iostream>
using namespace std;

void bubbleSort(int[], int&, int&);
void inserionSort (int [], int&, int&);
void generateRandomArray ( int [], int [], int []);
void selectionSort (int[], int&, int&);

const int length=5000;



  int main()
    {
        int compBubbleSort =0;
        int assignBubbleSort=0;
        int list1[length], list2 [length], list3[length];
    generateRandomArray( list1, list2, list3);

    cout<<"****************Bubble sort*****************"<<endl;

    bubbleSort( list1, compBubbleSort, assignBubbleSort);

    cout<<endl;
    cout<<"Number of compBubbleSort are :"<< compBubbleSort<<endl;
    cout<<"Number of  Assignments are :"<< assignBubbleSort<<endl;
    cout<<endl;
    compBubbleSort = 0;
    assignBubbleSort = 0;
    cout<<"**********Selection sort***************"<<endl;
    cout<<endl;

    selectionSort (list2, compBubbleSort, assignBubbleSort);

    cout<<"Number of compBubbleSort are:"<< compBubbleSort <<endl;
    cout<<"Number of  Assignments are :"<< assignBubbleSort<<endl;
    cout<<endl;
    compBubbleSort=0;
    assignBubbleSort=0;
    cout<<"****************Insertion sort*******************"<<endl;
    cout<<endl;

    inserionSort (list2, compBubbleSort, assignBubbleSort);
    cout<<"Number of compBubbleSort are:"<< compBubbleSort <<endl;
    cout<<"Number of  Assignments are :"<< assignBubbleSort<<endl;

    system ("pause");
    return 0;
}

void generateRandomArray( int list1[], int list2 [], int list3[])
{
    srand(time_t(0));

    for(int i=0; i<length; i++)
        list1 [i]= list2[i]= list3[i]=rand()%20000;

}

void bubbleSort (int num[], int &compBubbleSort, int &assignBubbleSort)
{
    for(int iter=1; iter<length; iter++)
    {
        for(int index=1; index<length; index++)
        {
            compBubbleSort++;
            if(num [index]>num[index+1])
            {
                int temp= num[index];
                num[index] = num[index+1];
                num[index+1]=temp;
                assignBubbleSort++;
            }
        }
    }
}

void seletionSort( int num[], int &compBubbleSort, int &assignBubbleSort)
{
    int index;
    int smallestIndex;
    int location;
    int temp;

    for (index=0; index<length-1; index++)
    {
        smallestIndex=index;

        for(location=index+1;location<length;location++)
        {
            compBubbleSort++;
            if(num[location]<num[smallestIndex])
            {
                smallestIndex=location;
            }
        }
         temp= num[smallestIndex];

         num[smallestIndex] =num[index];
         assignBubbleSort= assignBubbleSort+3;
         num[index] = temp;
    }
}

void inserionSort (int num[], int &compBubbleSort, int&assignments)
{
    int firstOutOfOrder,location;
    int temp;

    for (firstOutOfOrder=1; firstOutOfOrder<length;firstOutOfOrder++)
    {
        if(num[firstOutOfOrder]<num[firstOutOfOrder-1])
        {
        compBubbleSort++;
        temp= num[firstOutOfOrder];
        location=firstOutOfOrder;
        assignments=assignments+2;

            do
            {
                num[location]=num[location-1];
                assignments++;
                location--;
                compBubbleSort++;
            }
            while(location>0 &&num[location-1]>temp);
            num[location]=temp;
            assignments++;

        }   
    }
}

有人可以解释错误2019,因为我的大多数程序都有它。

1 个答案:

答案 0 :(得分:1)

您的函数定义中存在拼写错误。

变化:

void seletionSort( int num[], int &compBubbleSort, int &assignBubbleSort)

为:

void selectionSort( int num[], int &compBubbleSort, int &assignBubbleSort)
//       ^

这就是您的链接器找不到selectionSort的原因。

相关问题