"错误 LNK2019: 未解析的外部符号 "公共: __thiscall: 构造函数" 问题

"error LNK2019: unresolved external symbol "public: __thiscall : constructor" concern

本文关键字:公共 问题 thiscall 符号 构造函数 LNK2019 错误 外部      更新时间:2023-10-16

我目前正在尝试用c++创建一个Module类和Student类的简单实现。这些课程将包含特定的模块和注册的个别学生。然而,每当我尝试创建Module或Student对象时,我似乎都无法绕过这个特定的错误消息。

这可能是我错过的一些简单的东西,但我已经上传了我的标题和下面的源文件。请帮帮我,这快把我逼疯了。提前谢谢。

学生.h:

#include "stdafx.h"
#include <string>
class Student {
public:
    Student(std::string, std::string, int);
    std::string getName() const { return name; }
    std::string getDegree() const { return degree; }
    int getLevel() const { return level; }
private:
    std::string name;
    std::string degree;
    int level;
};

module.cpp:

// student.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "module.h"
#include <vector>
#include <iostream>
#include <string>
using namespace std;
void Module::enrol_student(Student studentY) {
    students.push_back(studentY);
}
void Module::attendance_register() {
    string nameX;
    cout << "Attendance Register:n" << endl;
    for (int i = 0; i < students.size(); i++) {
        Student studentX = students.at(i);
        nameX = studentX.getName();
        cout << nameX << endl;
    }
}

模块.h:

#include "stdafx.h"
#include "student.h"
#include <string>
#include <vector>
class Module {
public:
    Module(std::string, std::string, std::vector<Student>);
    std::string getModCode() { return modCode; }
    std::string getModTitle() { return modTitle; }
    void attendance_register();
    void enrol_student(Student);

private:
    std::string modCode;
    std::string modTitle;
    std::vector<Student> students;
};

testCode.cpp

// testCode.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

#include "module.h"
#include <vector>
#include <iostream>
#include <string>
using namespace std;
int main() {
    //Initial Test Data
    Student student1("Arthur Smith", "Computer Science", 1);
    return 0;
}

您需要定义在类中声明的构造函数。在student.cpp中,你需要这样的东西:

Student::Student(std::string name, std::string degree, int level) : name(name), degree(degree), level(level)
{
}

这将使用提供的值初始化成员。

模块也是如此。