search

Home  >  Q&A  >  body text

c++程序编译时出现 error LNK2019,无法解析的外部符号在 main中被引用

这是main文件main.cpp,信息查询

#include "sys.cpp"
#pragma comment(lib,"ws2_32.lib")
using std::cout;
using std::cin;
using std::endl;

int main(int argc, char const *argv[])
{
    cout << "请选择你要操作的内容:" <<  endl;
    cout << "1 学生基本" << endl;
    cout << "2 教师基本信息录入" << endl;
    cout << "3 学生信息查询" << endl;
    cout << "4 教师信息查询" << endl;
    cout << "5 " << endl ;
    int i;
    cin >>i;

    while(i!=5){
            int in_id; 
            char* in_name="n"; 
            char* in_sex = "n";
            int in_age; 
            int in_phone;
            char* in_job = "n";

            if (i==1)
            {
                cout << "学号: 姓名: 性别: 年龄: 电话: "<<endl;
                cin >> in_id >> in_name >> in_sex >> in_age >> in_phone ;
                Student student_temp(in_id, in_name, in_sex, in_age, in_phone);
                break;
            }

            else if (i==2)
            {
                cin >> in_id >> in_name >> in_sex >> in_age >> in_phone >> in_job;
                cout << "工号: 姓名: 性别: 年龄: 电话: 职称: " <<endl;
                Teacher teacher_temp(in_id, in_name, in_sex, in_age, in_phone, in_job);
                break;
            }

在编译的时候,会报出fatal error2019.


我把创建对象的构造函数注释掉再编译就没有问题,那么问题就出在构造函数。

这是在sys.h头文件里构造器的声明。

#ifndef _SYS_H
#define _SYS_H

class Person
{
    int ID ;
    char *name;
    char *sex;
    int age;
    int phone;
public:
    Person(int in_id, char* in_name, char* in_sex, int in_age, int in_phone);
    Person();
    ~Person();
class Student : public Person
{
public:
    Student(int in_id, char* in_name, char* in_sex, int in_age, int in_phone);
    void static  StudentSearch(int);
    ~Student(); 
};

class Teacher : public Person
{
    char *job;
public:
    Teacher(int in_id, char* in_name, char* in_sex, int in_age, int in_phone, char* in_job);
    void static  TeacherSearch(int);
    ~Teacher();
};
#endif

这是sys.cpp文件里构造器的定义。

Person::Person(int in_id, char* in_name, char* in_sex, int in_age, int in_phone)
{
    ID = in_id;
    name = in_name;
    sex = in_sex;
    age = in_age;
    phone = in_phone;
}

Student::Student(int in_id, char* in_name, char* in_sex, int in_age, int in_phone) : Person(in_id, in_name, in_sex, in_age, in_phone)
{
    ofstream data("Student data.txt"); 
    data << in_id << in_name << in_sex << in_age << in_phone << endl;
}
Teacher::Teacher(int in_id, char* in_name, char* in_sex, int in_age, int in_phone, char* in_job):Person(in_id, in_name, in_sex, in_age, in_phone)
{
    job = in_job;
    ofstream data("Teacher data.txt"); 
    data << in_id << in_name << in_sex << in_age << in_phone << in_job <<endl;
}

为什么会出现这样的错误呢?在网上找了许多相关的解决方法但是并没有成功。

巴扎黑巴扎黑2806 days ago686

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 13:52:32

    The link error means that you declared the prototype in the header file, but did not define it.
    According to your header file declaration
    Person’s constructor Person(); and Teacher’s corresponding destructor are not defined. First write these two implementations in cpp

    reply
    0
  • PHPz

    PHPz2017-04-17 13:52:32

    Change #include "sys.cpp" in the file main.cpp to #include "sys.h" and try

    reply
    0
  • Cancelreply