Home > database >  Undefined reference to studentType::studentType()
Undefined reference to studentType::studentType()

Time:04-21

I'm trying to learn how to use class in cpp, and one of the activities had me make a class header and a separate cpp file for implementation.

I was able to figure out what to do, but when I tried to compile it the compiler finished building with an error.

here are my codes:

main.cpp

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

using namespace std;

int main()
{
    studentType student;
    studentType newStudent("Brain", "Johnson", '*', 85, 95, 3.89);

    student.print();
    cout << "***************" << endl << endl;

    newStudent.print();
    cout << "***************" << endl << endl;

    return 0;
}

studentType.h

#ifndef CODINGPROJECTS_STUDENTTYPE_H
#define CODINGPROJECTS_STUDENTTYPE_H

#include <string>
using namespace std;

class studentType {

private: 

    string firstName;
    string lastName;
    char courseGrade;
    int testScore;
    int programmingScore;
    double GPA;

public:
    //GETTERS
    string getfirstName() const;
    string getlastName() const;
    char getcourseGrade() const;
    int gettestScore() const;
    int getprogrammingScore() const;
    double getGPA() const;

    //SETTERS
    void setfirstName(string name);
    void setlastName(string name);
    void setcourseGrade(char course);
    void settestScore(int test);
    void setprogrammingScore(int programming);
    void setGPA(double gpa);

    studentType(string first, string last, char course, int test, int programming, double gpa);
    studentType();
    void print();
};

#endif

studentTypeImp.cpp

#include <iostream>
#include "studentType.h"

using namespace std;

studentType::studentType(string first, string last, char course, int test, int programming, double gpa){

}

void studentType::setfirstName(string first){
    firstName = first;
}
void studentType::setlastName(string last){
    lastName = last;
}void studentType::setcourseGrade(char course) {
    courseGrade = course;
}
void studentType::settestScore(int test) {
    testScore = test;
}
void studentType::setprogrammingScore(int programming){
    programmingScore = programming;
}
void studentType::setGPA(double gpa){
    GPA = gpa;
}

string studentType::getfirstName() const{ return firstName;}

string studentType::getlastName() const{ return lastName;}

char studentType::getcourseGrade() const{return courseGrade;}

int studentType::gettestScore() const{ return testScore;}

int studentType::getprogrammingScore() const{ return programmingScore;}

double studentType::getGPA() const{return GPA; }

void studentType::print(){
    cout << "Name: " << getfirstName() << " " << getlastName() << endl;
    cout << "Grade: " << getcourseGrade() << endl;
    cout << "Test Score: " << gettestScore() << endl;
    cout << "Programming Score: " << getprogrammingScore() << endl;
    cout << "GPA: " << getGPA() << endl;
}

I have an inkling that it has to do with my header file but I don't know where to start.

CodePudding user response:

Add a definition for that default constructor which you declared in the header file. One way to do this is to add this code to studentTypeImp.cpp:

studentType::studentType() {
}

CodePudding user response:

Add a defalut constructor and use the command g main.cpp studentTypeImp.cpp to have a try.

  • Related