c++ - "1 unresolved externals"C++

标签 c++ visual-c++

我已经检查了我所有文件之间的连接,以及类和函数定义,但每次我尝试运行我的程序时它都会阻止我并告诉我它有“1 个 Unresolved external 问题”。

该程序应该打开多个文件(一个“学生”文件和一个“成绩”文件),从中读取,然后使用“查询文件”查看数据,找到要求的学生在查询文件中,并将其打印到新文件中。令人困惑?是的。

我仍然非常接近解决方案,但因为我使用的是 Visual Studio,所以在我找到并结束“1 个 Unresolved external 问题”之前,它甚至不允许我运行该程序。它甚至不会告诉我错误在哪里。我是 C++ 的新手,无论我多么努力地搜索,我似乎都无法解决这个问题。

这是我的主程序:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>

#include "Student.h"

using namespace std;

int main(){



    //Request a file to read from, and open it (I haven't declared the files yet, don't worry about that)
    ifstream input_file;
    string studentfile = "";
    string gradefile = "";
    string queryfile = "";


    //Create a Map for the Students
    map<string, Student *> student_map;

    //Open the Student file and load it in
    cout << "Loading \"" << studentfile << "\"... " <<endl;
    input_file.open(studentfile);

    //Look for:
    string id_number;
    string name;
    string address;
    string phone;

    //Boolean value to check for duplicate students
    bool duplicate = false;

    //Check to see if the Student File is empty
    if (!input_file.eof()){ 

        while (input_file.good()){
            //Get the ID Number
            input_file.get();
            getline (input_file, id_number);

            //Sort through and make sure there are no duplicate students
            for (map<string, Student *>::iterator counter = student_map.begin(); counter != student_map.end(); counter ++){
                if (counter->first == id_number ){
                    duplicate = true;
                    counter = student_map.end();
                }
            }

            if (duplicate != true){
                //Get the name
                input_file.get();
                getline (input_file, name);

                //Get the Address
                input_file.get();
                getline (input_file, address);

                //Get the Phone Number
                input_file.get();
                getline (input_file, phone);

                //Create a new student                                                          
                Student * newStudent = new Student (id_number, name, address, phone);

                //Add it to the map (referenced by the ID number)
                student_map[id_number] = newStudent;
            }
        }
    }

    else {
        return 0;
    }
    input_file.close();

    //Open the Grades file and load it in
    cout << "Loading \"" << gradefile << "\"... " <<endl;
    input_file.open(gradefile);

    //Look for (id_number already defined):
    string course;
    string grade;

    if (!input_file.eof()){

        while (input_file >> course >> id_number >> grade){
            //Find the student referenced
            Student* current_student = student_map[id_number];
            //Calculate their grade points and add them to their grade point vector
            current_student ->add_grade_points(current_student ->check_grade_points(grade));
        }
    }

    else {
        return 0;
    }
    input_file.close();

    //Open the Query file and load it in
    cout << "Loading \"" << queryfile << "\"... " << endl;
    input_file.open(queryfile);

    if (!input_file.eof()){

        //Write to
        ofstream output_file ("report.txt");

        //No need to "Look for" anything, id_number alread defined

        //If the file is open, write to it
        if (output_file.is_open())
        {
            while (input_file >> id_number)
            {
                //Print the ID Number (With four spaces)
                output_file << id_number << "    ";

                //Print out the GPA (With four spaces)
                Student* current_student = student_map[id_number];
                output_file << current_student->get_gpa() << "    ";

                //Print out the student's name (Then end that line)
                output_file << current_student->get_name() << endl;
            }
        }
        input_file.close();
        output_file.close();
    }

    else {
        return 0;
    }


    cout << endl;
    cout << "File Printed.";

    return 0;
}

这是我的“学生”类(头文件):

#pragma once
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>
#include <map>

using namespace std;

class Student
{
public:
    Student (string _id_number, string _name, string _address, string _phone);

    void add_grade_points (double grade_points);
    double check_grade_points (string grade);

    double get_gpa () const;
    string get_name () const;

private:
    string id_number;
    string name;
    string address;
    string phone;

    vector <double> grade_points;
};

最后,我的类函数:

#include "Student.h"

Student::Student (string _id_number, string _name, string _address, string _phone){
    id_number = _id_number;
    name = _name;
    address = _address;
    phone = _phone;
}

void Student::add_grade_points (double new_grade_point){
    grade_points.push_back(new_grade_point);
}

double Student::check_grade_points (string grade) {
    if (grade == "A")
        return 4.0;
    else if (grade == "A-")
        return 3.7;
    else if (grade == "B+")
        return 3.4;
    else if (grade == "B")
        return 3.0;
    else if (grade == "B-")
        return 2.7;
    else if (grade == "C+")
        return 2.4;
    else if (grade == "C")
        return 2.0;
    else if (grade == "C-")
        return 1.7;
    else if (grade == "D+")
        return 1.4;
    else if (grade == "D")
        return 1.0;
    else if (grade == "D-")
        return 0.7;
    else
        return 0.0;
}

double Student::get_gpa() const{
    //Add up all of the grade points
    double total = 0;

    for (int i = 0; i < grade_points.size(); i++) {
        total = total + grade_points[i];
    }

    //Calculate the Grade Point Average
    double gpa = total/grade_points.size();

    return gpa;
}

string Student::get_name() const{
    return name;
}

任何帮助都会非常棒!

编辑:这是出现的错误消息:

1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>C:\Users\Student\Documents\Visual Studio 2010\Projects\CS235 Project 1\Debug\CS235 Project 1.exe : fatal error LNK1120: 1 unresolved externals

最佳答案

您正在尝试将具有标准入口点 (int main()) 的程序编译为 Windows GUI 应用程序,而 Windows GUI 应用程序需要

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmd, int nShow)

相反。

不过,将您的入口点更改为此对您没有帮助,因为您正在尝试编译(在 Visual Studio 世界中所谓的)“控制台程序”。当您设置项目时,会有一个创建控制台项目的选项。您可以废弃现有的并创建一个新的控制台项目以将您的代码粘贴到其中以便轻松修复。不过,如果您真的很喜欢当前的项目,则可以修复它。

在项目设置中: 在 Configuration Properties > C/C++ > Preprocessor 下,更改 _WINDOWS; _CONSOLE 预处理器定义中的字符串; 在 Configuration Properties > Linker > System 下,将 SubSystem 更改为 Console。

关于c++ - "1 unresolved externals"C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8936860/

相关文章:

c++ - VC 2010 上带 SSL 的 libpq

c++ - 关闭部分代码的优化

c++ - 重载运算符时可以使用标志吗?

c++ - 如何检查C中是否存在文件:\drive using VC++?

visual-c++ - 如何更改某些文件类型的图标?

c++ - 试图取消引用迭代器的段错误

c++ - 如何检查 Windows 是嵌入式还是 POS?

c++ - 如何在 Qt Widgets 中删除带有插槽的按钮

c++ - 通过C++中的引用声明传递

c++ - 非类型模板参数 : how to pass reference to base class object?