未解决的外部符号问题?

时间:2014-02-07 07:12:30

标签: c++

所以第一次发帖和真正的初学者,所以希望这样做,但真的需要帮助这个程序。

谢谢大家的帮助,我做了一些修改,但这些错误消失了,但现在我得到了这些:

错误5错误LNK2019:未解析的外部符号“void __cdecl searchID(struct StudentRecord *,int)”(?searchID @@ YAXPAUStudentRecord @@ H @ Z)在函数_main中引用

错误6错误LNK2019:未解析的外部符号“void __cdecl sortRecordsByName(struct StudentRecord *,int)”(?sortRecordsByName @@ YAXPAUStudentRecord @@ H @ Z)在函数_main中引用

错误7错误LNK2019:未解析的外部符号“void __cdecl getInformation(struct StudentRecord * const,int)”(?getInformation @@ YAXQAUStudentRecord @@ H @ Z)在函数_main中引用

错误8错误LNK1120:3个未解析的外部

以下是修订后的代码:

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

const int MAX_SIZE = 20;

struct Answers
{
    char answer1;
    char answer2;
    char answer3;
    char answer4;
    char answer5;
};

struct StudentRecord
{
    int ID;
    char student_name[MAX_SIZE];
    Answers answer;
    double score;
    double average;
    char letter_grade;
};

void getInformation(StudentRecord student[], int&);
void enterKey(char[]);
void calculateAvgAndLetter(StudentRecord *student, int, char[]);
void sortRecordsByName(StudentRecord * student, int);
void displayResults(StudentRecord * student, int);
bool displayReport(StudentRecord * student, int);
void searchID(StudentRecord * student, int);




int main()
{
    int search;
    int ID = 0;
    bool check = false;
    char repeat = 'y';

    const int MAX_STUDENTS = 10;
    int number_of_students = 0;
    char key[5];

    do {
        cout << "How many students: ";
        cin >> number_of_students;

        StudentRecord student[MAX_STUDENTS];
        enterKey(key);
        getInformation(student,number_of_students);
        calculateAvgAndLetter(student, number_of_students, key);
        sortRecordsByName(student, number_of_students);
        displayResults(student, number_of_students);

        cout << "Would you like to search for a student (y for yes and n for no)?: ";
        cin >> search;

        while (search != 'n' && search != 'y') 
        {
            cout << "Wrong ID" << endl;
            cout << "Would you like to search for another student (y for yes and n for no)?: ";
            cin >> search; 
        }

        if (search == 'y') 
        {
            searchID(student, number_of_students);
            check = true; 
        }
        else
            check = false;


        while (check)
        {
            cout << "Would you like to search for another student (y for yes and n for no)?: ";
            cin >> search;

            while (search != 'n' && search != 'y') 
            {
                cout << "Wrong ID" << endl;
                cout << "Would you like to search for another student (y for yes and n for no)?: ";
                cin >> search; 
            }

            if (search == 'y')
            {
                searchID(student, number_of_students);
                check = true;
            }
            else
                check = false;

        }

        cout << "Would you like to process another group of students(y for yes and n for no)?: " << endl;
        cin >> repeat;

    } while (repeat == 'y');

    return 0;
}

void enterKey(char key[5])
{
    for (int i = 0; i < 6; i++) {
        cout << "Please enter the answer to question " << i + 1 << ": ";
        cin >> key[i]; }
}

void getInformation(StudentRecord *student[],int number_of_students)
{
    int max_students;
    char again;
    int i = 0;

    cout << "Would you like to enter student information? " << endl;
    cin >> again;

    if (again == 'Y' || again == 'y')
    {
        do
        {
            cout << "Please enter student " << i + 1 << " information:" << endl;
            cout << "Please Enter Student Name Last name first with no spaces: " << endl;
            cin >> student[i]->student_name;
            cout << "Please Enter Student Id Number: " << endl;
            cin >> student[i]->ID;

            cout << "Please enter the student's answer to question 1: ";
            cin >> student[i]->answer.answer1;
            cout << "Please enter the student's answer to question 2: ";
            cin >> student[i]->answer.answer2;
            cout << "Please enter the student's answer to question 3: ";
            cin >> student[i]->answer.answer3;
            cout << "Please enter the student's answer to question 4: ";
            cin >> student[i]->answer.answer4;
            cout << "Please enter the student's answer to question 5: ";
            cin >> student[i]->answer.answer5;
            i++;

            if (i < number_of_students) {
                cout << "Would you like to enter student information? " << endl;
                cin >> again;
            }
            else again='n';
        }

        while(again == 'y' || again == 'Y');
        cout<<"Reports:"<<endl;
    }
}

void sortByID(StudentRecord student[], int number_of_students)
{
    bool swap = true;
    int j = 0;
    int temp;

    while (swap) 
    {
        swap = false;
        j++;

        for (int i = 0; i < number_of_students - j; i++)
        {
            if (student[i].ID > student[i + 1].ID) 
            {
                temp = student[i].ID;
                student[i].ID = student[i + 1].ID;
                student[i + 1].ID = temp;
                swap = true;
            }
        }
    }
}

int LinearSearch(StudentRecord student[], int number_of_students, int ID, int first, int last)
{
    int i = 0;
    int position;

    for (int i = 0; i < number_of_students; i++) 
    {

        if (ID == student[i].ID) {
            position = i; }
    }

    return position;
}

void SortRecordsByName(StudentRecord student[], int number_of_students)
{
    bool swap = true;
    int j = 0;
    StudentRecord temp;

    while (swap) 
    {
        swap = false;
        j++;
        for (int i = 0; i < number_of_students - j; i++) 
        {
            if (student[i].student_name > student[i + 1].student_name) 
            {
                strcpy(temp.student_name,student[i].student_name);
                strcpy(student[i].student_name,student[i + 1].student_name);
                strcpy(student[i + 1].student_name,temp.student_name);
                swap = true;
            }
        }
    }
}

void calculateAvgAndLetter(StudentRecord student[], int number_of_students, char key[5])
{
    int points1;
    int points2;
    int points3;
    int points4;
    int points5;
    int i = 0;
    int j = 0;

    for (int i = 0; i < number_of_students; i++) 
    {
        if (student[i].answer.answer1 == key[0])
            points1 = 10;
        else
            points1 = 0;

        if (student[i].answer.answer2 == key[1])
            points2 = 10;
        else
            points2 = 0;

        if (student[i].answer.answer3 == key[2])
            points3 = 10;
        else
            points3=0;

        if (student[i].answer.answer4 == key[3])
            points4 = 10;
        else
            points4 = 0;

        if (student[i].answer.answer5 == key[4])
            points5 = 10;
        else
            points5 = 0;

        student[i].score= points1 + points2 + points3 + points4 + points5;
        student[i].average = student[i].score * 2 ;


        if((student[i].average >= 90) && (student[i].average <= 100))
            student[i].letter_grade='A';

        else if ((student[i].average >= 80) &&(student[i].average <= 89))
            student[i].letter_grade='B';

        else if ((student[i].average >= 70) && (student[i].average <= 79))
            student[i].letter_grade='C';

        else if ((student[i].average >= 60) && (student[i].average <= 69))
            student[i].letter_grade='D';

        else if ((student[i].average >= 0) && (student[i].average <= 59))
            student[i].letter_grade='F'; }
}

void displayResults(StudentRecord student[], int number_of_students)
{
    cout << fixed<< setprecision(2);
    cout << "Student ID" << setw(10) << "StudentName" << setw(10) << "Answers" << setw(10) <<"Total Pts" << setw(10) << "Average" << setw(12) << "Letter Grade"<<endl;
    cout << setfill('-');
    cout << setw(50) << "-" << endl;
    cout << setfill(' ');

    for (int i = 0; i < number_of_students; i++)
        cout << setw(3) << student[i].ID << setw(15) << student[i].student_name << setw(10) << student[i].answer.answer1 << student[i].answer.answer2 << student[i].answer.answer3 << student[i].answer.answer4
        << student[i].answer.answer5 << setw(9) << student[i].score << setw(10) << student[i].average << setw(13) << student[i].letter_grade << endl;

    cout <<"\n\n\nStudents admitted to graduate program: \n" << endl;
    cout << fixed<< setprecision(2);
    cout << "Student ID" << setw(10) << "StudentName" << setw(10) <<"Total Pts" << setw(10) << "Average" << setw(12) << "Letter Grade"<<endl;
    cout << setfill('-');
    cout << setw(40) << "-" << endl;
    cout << setfill(' ');

    for (int i = 0; i < number_of_students; i++) 
    {
        if (student[i].letter_grade == 'A' || student[i].letter_grade == 'B') {
            cout << setw(2) << student[i].ID << setw(12) << student[i].student_name << setw(10) << student[i].score
                << setw(10) << student[i].average << setw(10) << student[i].letter_grade << endl; }
    }

    cout <<"\n\n\nStudents with Conditional Admission to Graduate Program: \n" << endl;
    cout << fixed<< setprecision(2);
    cout << "Student ID" << setw(10) << "StudentName" << setw(10) <<"Total Pts" << setw(10) << "Average" << setw(12) << "Letter Grade"<<endl;
    cout << setfill('-');
    cout << setw(40) << "-" << endl;
    cout << setfill(' ');

    for (int i = 0; i < number_of_students; i++) 
    {
        if (student[i].letter_grade == 'C') {
            cout << setw(2) << student[i].ID << setw(10) << student[i].student_name << setw(10) << student[i].score
                << setw(10) << student[i].average << setw(10) << student[i].letter_grade << endl; }
    }

    cout <<"\n\n\nStudents Not Allowed Admission: \n" << endl;
    cout << fixed<< setprecision(2);
    cout << "Student ID" << setw(10) << "StudentName" << setw(10) <<"Total Pts" << setw(10) << "Average" << setw(12) << "Letter Grade"<<endl;
    cout << setfill('-');
    cout << setw(40) << "-" << endl;
    cout << setfill(' ');

    for (int i = 0; i < number_of_students; i++) 
    {
        if (student[i].letter_grade == 'D' || student[i].letter_grade == 'F') {
            cout << setw(2) << student[i].ID << setw(10) << student[i].student_name << setw(10) << student[i].score
                << setw(10) << student[i].average << setw(10) << student[i].letter_grade << endl; }
    }
}

void searchIDandDisplay(StudentRecord student[], int number_of_students, int ID)
{
    bool check = true;
    string acceptence;

    cout << "\n\n\nEnter the ID of the student: ";
    cin >> ID;


    while (check) 
    {
        for(int i = 0; i < number_of_students; i++)
        {
            if (ID == student[i].ID) 
            {
                check = false; 
            }
        }
        if (check == true) 
        {
            cout << "No student with this ID" << endl;
            cin >> ID;
        }
    }


    int i = LinearSearch(student, number_of_students, ID, student[0].ID, student[number_of_students].ID);

    if (student[i].letter_grade == 'A' || student[i].letter_grade == 'B' || student[i].letter_grade == 'C') {
        acceptence = "Accepted"; }
    else
        acceptence = "Denied";


    cout << fixed << setprecision(2);
    cout << "Student ID" << setw(10)
         << "StudentName"
         << setw(10) << setw(10)
         <<"Total Pts" << setw(10)
         << "Average" << setw(10)
         << "Letter Grade"
         << "Status" << endl;
    cout << setfill('-');
    cout << setw(48) << "-" << endl;
    cout << setfill(' ');

    cout << setw(2)  << student[i].ID
         << setw(10) << student[i].student_name
         << setw(10) << student[i].score
         << setw(10) << student[i].average
         << setw(10) << student[i].letter_grade
         << setw(10) << acceptence <<endl;

} 

就像我说一个真正的初学者所以任何帮助将不胜感激,并希望我发布正确,抱歉,如果有什么不对。再次感谢大家的帮助!

5 个答案:

答案 0 :(得分:0)

void getInformation(StudentRecord * student [],int&amp; num_std);

这表示getInformation获取了一系列指向StudentRecord的指针。你只想

void getInformation(StudentRecord student [],int&amp; num_std);

带有一系列StudentRecords。

答案 1 :(得分:0)

void getInformation(StudentRecord *student[], int& num_std);

在此声明中,student被视为指向StudentRecord的指针。将其更改为:

void getInformation(StudentRecord *student, int& num_std);

void getInformation(StudentRecord student[], int& num_std);

答案 2 :(得分:0)

问题在于这一行

getInformation(student,number_of_students);  

此处student的类型为StudentRecord[],但getInforamtion的参数键入StudentRecord[]*。为了使类型排成一行,您需要将参数类型更改为StudentRecord*或传递student的地址。

鉴于您将大小与值集合一起传递,我倾向于将getInformation的签名更改为

void getInformation(StudentRecord *student, int& num_std);

答案 3 :(得分:0)

一些评论


int search;
int ID = 0;
bool check = false;
char repeat = 'y';

const int MAX_STUDENTS = 5;
int number_of_students = 0;
char key[5];

do {
    cout << "How many students: ";
    cin >> number_of_students;          

在继续

之前,您应该先检查if (number_of_students < MAX_STUDENTS)

你的数组键的维度为5,但在你的for循环中,你从0..5开始是6,最好还是将维度作为函数enterKey(char *key, int maxsize) { ... }的参数

void enterKey(char key[5]) -> (char* key, int maxsize)
{
    for (int i = 0; i < 6; i++)  -> for (int i=0; i<maxsize; ++i)
    {
        cout << "Please enter the answer to question " << i + 1 << ": ";
        cin >> key[i]; 
    }
}

形式参数指定StudentRecord 指针数组

void getInformation(StudentRecord *student[],int number_of_students)

但你没有以这种方式提供:

StudentRecord student[MAX_STUDENTS];
enterKey(key);
getInformation(student,number_of_students);

如果您按上述方式编写,则student是一个StudentRecords数组

相反,您应该将原型更改为这样(记住数组衰减为ptr):

void getInformation(StudentRecord *student,int number_of_students)

另一个提示

你的struct Answers有5个答案字符answer1,answer2,..将它作为一个数组更方便,那么你可以缩短你输入答案的方式

答案 4 :(得分:0)

您的原型不需要其参数的名称。他们只需要数据类型。

void getInformation(StudentRecord*, int&);
void enterKey(char[]);
void calculateAvgAndLetter(StudentRecord*, int, char[]);
void sortRecordsByName(StudentRecord*, int);
void displayResults(StudentRecord*, int);
bool displayReport(StudentRecord*, int);
void searchID(StudentRecord*, int);

getInformation的原型:

void getInformation(StudentRecord *student[], int& num_std);

将其第二个参数键入为int&amp;,而您的定义为:

void getInformation(StudentRecord *student[],int number_of_students)

缺少&amp ;.在这种情况下,您可以删除&amp;从原型或将其添加到定义中,您当前的实现应该是相同的。

正如其他人所说,你夸大了第一个参数。尝试删除[]或*(无论如何总是通过引用传递数组)。