0

I need help debugging this code for a class. I am not a computer science major, so I need explanation in very basic terms. The error code I'm getting is error LNK2019: unresolved external symbol "public: void __thiscall Student::showStudent(void)" (?showStudent@Student@@QAEXXZ) referenced in function _main

I don't understand what this means. Can someone explain please?

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

It's giving me a LNK2019 error? Help please!

A.O.
  • 1
  • 1

1 Answers1

1

Adding to AndyG's answer. Your Student class should inherit from Person. Like so:

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

Because in your Student constructor you're calling Person::Person(id, a, i); and this will not set your Student attributes idNum, age and initial, because you've redefined them in Student and you're calling the Person ctor without initialising it to anything.

To make your code more cohesive and reusable I would define the classes like this:

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.

So you set your Student constructor like this:

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

And now you shouldn't have errors and you have a clean, reusable OOP solution at hand. You could now, for instance, have a Teacher which also inherits (is a generalisation of) from Person and it will have Person's attributes.

Community
  • 1
  • 1
Santiago Varela
  • 2,199
  • 16
  • 22
  • 1
    Agreed, good call. It appears that OP instead re-defined the member variables for `Person` as member variables for `Student` – AndyG Mar 21 '17 at 17:20