编译时未解决的外部符号错误 [duplicate]

Unresolved external symbol error when compiling [duplicate]

提问人:dawa 提问时间:2/18/2016 最后编辑:Vlad from Moscowdawa 更新时间:2/18/2016 访问量:832

问:

当我尝试编译它时,我收到一条错误消息

 unresolved external symbol "void __cdecl showarray(int * const,int)"

不确定我做错了什么?我该怎么办,我的代码看起来像这样

#include<iostream>
#include<string>

using namespace std;


void sortArray(int [], int );
void showarray(int [],int );

int main(){
    int endtime[10] = { 70, 38, 8, 101, 11, 127, 313, 14, 16, 127 };

    cout << "unsorted" << endl;
    showarray(endtime, 10);

    sortArray(endtime, 10);

    cout << "the sorted value are :" << endl;
    showarray(endtime, 10);

    return 0;
}

void sortArray(int array[], int size){
    bool swap;
    int temp;

    do{
        swap = false;
        for (int count = 0; count < (size - 1); count++){
            if (array[count] > array[count+1]){
                temp = array[count];
                array[count] = array[count + 1];
                array[count + 1] = temp;
                swap = true;
            }
        }
    } while (swap);
}

void showarray(const float array[], int size){
    for (int i = 0; i < size; i++)
        cout << array[i] <<" "<< endl;
}
C++ 数组函数 bubble-sort unresolved-external

评论

2赞 Some programmer dude 2/18/2016
每当你遇到这样的错误,并且你知道你已经定义了函数时,请仔细查看函数定义并将其与声明进行比较,也许复制声明并将其放在定义上方,这样你就可以同时查看两者。Doint 会立即显示任何差异。
0赞 Rob K 2/18/2016
这也是我尝试在函数使用之前始终定义函数并避免预先声明的原因之一。单独的声明是代码复制的一种形式,必须与定义保持同步。不要重复自己!
0赞 Niall 2/18/2016
stackoverflow.com/a/12574403/3747990

答:

1赞 Anedar 2/18/2016 #1

您声明的

void showarray(int [],int );

但是定义一个

void showarray(const float array[], int size)

两者是不一样的。当编译器尝试编译函数调用时,他只知道第一个函数调用并使用它。但是链接者只能找到第二个,这就是他产生错误的原因。

评论

0赞 dawa 2/18/2016
哎呀,我真傻,太感谢了!
0赞 shafeen 2/18/2016 #2

函数声明需要与函数定义匹配:

void showarray(int [],int );

void showarray(const float array[], int size){
    for (int i = 0; i < size; i++)
       cout << array[i] <<" "<< endl;

}

需要有匹配的函数签名。

评论

0赞 dawa 2/18/2016
谢谢!现在一切都修好了。
1赞 Vlad from Moscow 2/18/2016 #3

首先,您声明了函数

void showarray(int [],int );

但定义了功能

void showarray(const float array[], int size)
{
    //...
}

更改声明和定义,例如

void showArray( const int [], int );
     ^^^^^^^^^  ^^^^^^^^^^^^

例如(函数定义)

void showArray( const int array[], int size )
{
    for ( int i = 0; i < size; i++ )
    {
        cout << array[i] << ' ';
    }
    cout << endl;
}

并且不要忘记使用 name 更改函数调用。showArray

考虑到变量应该在 if 语句中声明。您也可以使用标准函数,例如tempstd::swap

std::swap( array[count], array[count+1] );

该程序可能如下所示

#include <iostream>

void sortArray( int a[], size_t n )
{
    while ( !( n < 2 ) )
    {
        size_t last = 0;
        for ( size_t i = 1; i < n; i++ )
        {
            if ( a[i] < a[i-1] )
            {
                int temp = a[i];
                a[i] = a[i-1];
                a[i-1] = temp;
                last = i;
            }
        }
        n = last;
    }
}

void showArray( const int a[], size_t n )
{
    for ( size_t i = 0; i < n; i++ ) std::cout << a[i] << ' ';
    std::cout << std::endl;
}

int main()
{
    const size_t N = 10;
    int endtime[N] = { 70, 38, 8, 101, 11, 127, 313, 14, 16, 127 };

    std::cout << "unsorted: ";
    showArray( endtime, N );

    sortArray( endtime, N );

    std::cout << "  sorted: ";
    showArray( endtime, N );
}        

它的输出是

unsorted: 70 38 8 101 11 127 313 14 16 127 
  sorted: 8 11 14 16 38 70 101 127 127 313 

使用标准功能,功能更紧凑、更清晰std::swapsortArray

void sortArray( int a[], size_t n )
{
    while ( not ( n < 2 ) )
    {
        size_t last = 0;
        for ( size_t i = 1; i < n; i++ )
        {
            if ( a[i] < a[i-1] )
            {
                std::swap( a[i], a[i-1] );
                last = i;
            }
        }
        n = last;
    }
}

评论

0赞 Vlad from Moscow 2/18/2016
@dawa 请注意,我的排序函数有拼写错误。必须有 last = i;而不是 last = i - 1;