“1未解析的外部” C ++ [英] "1 unresolved externals" C++

查看:137
本文介绍了“1未解析的外部” C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经检查了所有的文件之间的连接,以及类和函数定义,但每次我尝试运行我的程序,它停止我,并告诉我,它有1未解决的外部。



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



仍然,我很接近一个解决方案,但因为我使用Visual Studio它甚至不会让我运行程序,直到我找到,结束1未解决的外部。它甚至不会告诉我错误在哪里。我对C ++很新,无论我搜索多么困难,我似乎无法解决这个问题。



这是我的主程序:

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

#includeStudent.h

using namespace std;

int main(){



//请求文件读取,然后打开它(我还没有声明文件,不用担心)
ifstream input_file;
string studentfile =;
string gradefile =;
string queryfile =;


//为学生创建地图
地图< string,Student *> student_map;

//打开Student文件并将其加载到
中cout<< 载入\<< studentfile<<\...<< endl;
input_file.open(studentfile);

//查找:
string id_number;
string name;
string address;
string phone;

//用于检查重复学生的布尔值
bool duplicate = false;

//检查学生文件是否为空
if(!input_file.eof()){

while(input_file.good()){
//获取ID号
input_file.get();
getline(input_file,id_number);

//通过并确保没有重复的学生
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){
//获取名称
input_file.get();
getline(input_file,name);

//获取地址
input_file.get();
getline(input_file,address);

//获取电话号码
input_file.get();
getline(input_file,phone);

//创建新学生
学生* newStudent =新学生(id_number,姓名,地址,电话)

//将其添加到地图(由ID号引用)
student_map [id_number] = newStudent;
}
}
}

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

//打开Grades文件并将其加载到
中cout<< Loading \<< gradefile<<\...<< endl;
input_file.open(gradefile);

//查找(id_number已定义):
string course;
string grade;

if(!input_file.eof()){

while(input_file>> course>> id_number>> grade){
//找到引用的学生
Student * current_student = student_map [id_number];
//计算他们的成绩点并将它们添加到他们的成绩点向量
current_student - > add_grade_points(current_student - > check_grade_points(grade));
}
}

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

//打开查询文件并将其加载到
cout<< Loading \<< queryfile<<\...< endl
input_file.open(queryfile);

if(!input_file.eof()){

//写入
ofstream output_file(report.txt);

//无需查找任何内容,id_number已定义

//如果文件已打开,请写入
if(output_file.is_open ())
{
while(input_file>> id_number)
{
//打印ID号(有四个空格)
output_file< id_number<< ;

//打印出GPA(有四个空格)
Student * current_student = student_map [id_number];
output_file<< current_student-> get_gpa()<< ;

//打印出学生的姓名(然后结束那一行)
output_file<< current_student-> get_name()<< endl
}
}
input_file.close();
output_file.close();
}

else {
return 0;
}


cout<< endl
cout<< 文件打印。

return 0;
}

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

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

using namespace std;

类学生
{
公众:
学生(字符串_id_number,字符串_name,串_address,串_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;

矢量< double> grade_points;
};

最后,我的类函数:

 的#includeStudent.h

::学生学生(字符串_id_number,字符串_name,串_address,串_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 {
//添加所有成绩点
double total = 0;

for(int i = 0; i total = total + grade_points [i];
}

//计算成绩点平均值
double gpa = total / grade_points.size();

return gpa;
}

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

任何帮助将是如此惊人!



EDIT:以下是出现的错误消息:

  1> MSVCRTD.lib(crtexew.obj) :错误LNK2019:无法解析的外部符号_WinMain @函数___tmainCRTStartup 
1 GT引用16; C:\Users\Student\Documents\Visual工作室2010\Projects\CS235项目1\Debug\CS235项目的1.exe:致命错误LNK1120:1无法解析的外部


解决方案

您正试图编译标准的入口点计划(INT主要())作为Windows GUI应用程序和Windows GUI应用程序需要

  INT WINAPI的WinMain(HINSTANCE HINST,HINSTANCE hPrevInst,LPSTR lpCmd,INT n显示)

相反



更改您的入口点这不会帮助你,虽然,因为你试图编译(什么叫做在Visual Studio世界)一个控制台程序 。当您设置项目时,将有一个选项来创建控制台项目。您可以删除您拥有的一个,并创建一个新的控制台项目来粘贴您的代码,以方便修复。如果你真的附加到当前项目虽然,你能解决这个问题。



在项目设置:
在配置属性> C / C ++>预处理,改变_WINDOWS;字符串在Preprocessor定义中为_CONSOLE;
,并在配置属性>链接器>系统下,将子系统更改为控制台。


I've checked the connections between all of my files, as well as Class and Function definitions, but every time I try to run my program it stops me and tells me that it has "1 unresolved externals".

The program is supposed to open up multiple files (A "student" file, and a "grades" file), read from them, and then use a "query file" to look through the data, find the students asked for in the query file, and print it to a new file. Confusing? Yes.

Still, I'm pretty close to a solution, but because I'm using Visual Studio it won't even let me run the program until I find and put an end to the "1 unresolved externals". It won't even tell me where the error is. I'm fairly new to C++, and no matter how hard I search, I can't seem to remedy the issue.

Here's my Main Program:

#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;
}

Here's my "Student" Class (Header File):

#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;
};

And finally, my class functions:

#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;
}

Any help would be so amazing!

EDIT: Here's the error message that comes up:

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

解决方案

You are trying to compile a program with the standard entry point (int main()) as a windows GUI application, and Windows GUI applications require

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

Instead.

Changing your entry point to this won't help you though, as you're trying to compile (what's called in the Visual Studio world) a 'console program'. When you set up the project, there would have been an option to create a console project. You can scrap the one you've got and create a new console project to paste your code into for an easy fix. If you're really attached to your current project though, you can fix it.

In the project settings: Under Configuration Properties > C/C++ > Preprocessor, change the _WINDOWS; string in the Preprocessor definitions to _CONSOLE; and under Configuration Properties > Linker > System, change the SubSystem to Console.

这篇关于“1未解析的外部” C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆