c++ - 错误代码 LNK2019

标签 c++ lnk2019

<分区>

我需要帮助调试这个类的代码。我不是计算机科学专业的,所以我需要非常基本的解释。我得到的错误代码是 error LNK2019: unresolved external symbol "public: void __thiscall Student::showStudent(void)"(?showStudent@Student@@QAEXXZ) referenced in function _main

我不明白这是什么意思。有人可以解释一下吗?

#include<iostream>
#include<string.h>

using namespace std;
//Added namespace
class Person
{
public:
char initial;
Person(const int id, const int a, const char i);
//Renamed function Person
void showPerson();
int idNum;
int age;
};

Person::Person(const int id, const int a, const char i)
{
idNum = id;
age = a;
initial = i;
};
void Person::showPerson(void)
{
cout << "Person id#: " << idNum << " Age: " << age << " Initial: " << initial;
//Added << to the correct places
}
class Student
{
public:
Student (const int id, const int a, const char i, const double gpa);
void showStudent();
double gpa;
int idNum;
int age;
char initial;
};
Student::Student(const int id, const int a, const char i, const double gradePoint)
{
Person::Person(id, a, i);
gpa = gradePoint;
};

void showStudent(Student student)
{
cout << "Student ID# " << student.idNum;
cout << " Avg: " << student.gpa << endl;
cout << "Person id#: " << student.idNum << " Age: " << student.age << " Initial: " << student.initial;
cout << endl << " Age: " << student.age << ".";
cout << " Initial: " << student.initial << endl;
};
void main()
{
Person per(387, 23, 'A');
//Put two lines into one
cout << endl << "Person..." << endl;
per.showPerson();
Student stu(388, 18, 'B', 3.8);
cout << endl << endl << "Student..." << endl;
stu.showStudent();
system("pause");
}

它给我一个 LNK2019 错误?请帮忙!

最佳答案

添加到 AndyG的回答。您的 Student 类应该继承自 Person。像这样:

class Student : public Person {
 /* members */
};

因为在您的 Student 构造函数中您正在调用 Person::Person(id, a, i); 并且这不会设置您的 Student 属性 idNum , age 和 initial,因为您已经在 Student 中重新定义了它们,并且您在调用 Person ctor 时没有将其初始化为任何东西。

为了使您的代码更具凝聚力和可重用性,我会这样定义类:

class Person {
      protected:
        char initial;
        int idNum;
        int age;
      public:
        Person(const int id, const int a, const char i);
        void showPerson();

};

class Student : public Person {
      private:
        double gpa;
      public:
        Student (const int id, const int a, const char i, const double gpa);
        void showStudent();

};

In this way your Student instances have all of the protected member attributes (id, age and initial) from the Person class and this defines a Student is a Person. This is part of Object Oriented Programming (OOP) paradigm.

所以你像这样设置你的 Student 构造函数:

Student::Student(const int& id, const int& a, const char& i, const double& gradePoint) {
    idNum = id; // This works because Student inherits from Person, so it has all its attributes!
    age = a;
    initial = i;
    gpa = gradePoint;
}; 

现在您不应该有错误,并且您手头有一个干净、可重用的 OOP 解决方案。例如,您现在可以拥有一个也从 Person 继承(泛化)的 Teacher,并且它将具有 Person 的属性。

关于c++ - 错误代码 LNK2019,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42933969/

相关文章:

c++ - 将 QtSoap 引用添加到 Qt Creator

c++ - std::function 的重载运算符?

c++ - 将 DCMTK 与 Visual Studio 结合使用时出现链接器错误 LNK2019

c++ - LNK2019 : unresolved extrenal symbol when using std::ifstream

c++ - 在 Visual C++ 中将 win32 .lib 与 x64 项目一起使用

c++ - 读取文本文件并在 C/C++ 中仅获取每行最后一个整数的结果

c++ - 自一周开始以来的秒数?

c++ - 与 Rcpp 相交函数

c++ - BulletPhysics:错误 LNK2001:未解析的外部符号

c++ - 错误LNK2019是什么意思