2

我对编程很陌生。我遇到了这个我无法弄清楚的错误。您应该能够输入分数,它将使用预先放入数组中的信息并告诉您有多少学生获得了该分数。

我得到的错误信息是:

1>------ Build started: Project: Ch11_27, Configuration: Debug Win32 ------
1>Build started 4/4/2013 1:17:26 PM.
1>InitializeBuildStatus:
1>  Touching "Debug\Ch11_27.unsuccessfulbuild".
1>ClCompile:
1>  main.cpp
1>main.obj : error LNK2019: unresolved external symbol "void __cdecl checkScore(int * const,int * const)" (?checkScore@@YAXQAH0@Z) referenced in function _main
1>F:\a School Stuff TJC Spring 2013\Intro Prog\C++ Projects\Ch11_27\Debug\Ch11_27.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.

这是我的代码:

//Advanced27.cpp - displays the number of students
//earning a specific score
//Created/revised by <your name> on <current date>

#include <iostream>
using namespace std;

//Function Prototypes
void checkScore( int scores[], int storage[]);

int main()
{
    //declare array
    int scores[20] = {90, 54, 23, 75, 67, 89, 99, 100, 34, 99, 
                      97, 76, 73, 72, 56, 73, 72, 20, 86, 99};
    int storage[4] = {0};
    char answer = ' ';

    cout << "Do you want to check a grade? (Y/N): ";
    cin >> answer;
    answer = toupper(answer);

    while (answer = 'Y')
    {
        checkScore(scores, storage);

    cout << "Do you want to check a grade? (Y/N): ";
    cin >> answer;
    answer = toupper(answer);

    }
    system("pause");
    return 0;
}   //end of main function

//*****Function Defenitions*****
void checkGrade(int scores[], int storage[])
{
    int temp = 0;
    int earnedScore = 0;

    cout << "Enter a grade you want to check: ";
    cin >> earnedScore;

    for (int sub = 0; sub <= 20; sub +=1)
    {
        if (scores[sub] = earnedScore)
        {
            storage[temp] += 1;

        }
    }
}
4

4 回答 4

4

问题是您的函数定义与函数声明的名称不同:

void checkScore( int scores[], int storage[]);
void checkGrade(int scores[], int storage[])

你需要选择一个或另一个。编译器收到您的调用checkScore并看到它没有定义。更改要调用的定义checkScore将修复它。

于 2013-04-04T18:40:52.190 回答
3

checkGrade()应该调用 main() 函数下方的函数void checkScore( int scores[], int storage[])

于 2013-04-04T18:41:31.003 回答
1

这意味着您声明了要命名的函数,checkScore但您定义了要命名的函数checkGrade。然后当main()尝试调用checkScore编译器时说“好的,上面已经声明了。即使我找不到它,我也会允许它。它可能在不同的库或源文件中。”。然后链接器有责任找到它。由于链接器找到checkGrade但未找到checkScore,因此链接器会抛出错误,指出未定义的引用(main()引用checkScore而不是checkGrade)。

于 2013-04-04T18:42:38.340 回答
0

看来您已经声明了您的功能

void checkScore( int scores[], int storage[]);

但实际上并没有定义它(给它一个函数体)。定义你的功能

void checkScore( int scores[], int storage[]){

}

使这个错误消失。

于 2013-04-04T18:41:30.663 回答