对 studentType::studentType() 的未定义引用
Undefined reference to studentType::studentType()
我正在尝试学习如何在 cpp 中使用 class,其中一项活动让我制作了一个 class header 和一个单独的 cpp 文件以供实施。
这是我的代码:
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;
}
我隐约知道它与我的 header 文件有关,但我不知道从哪里开始。
为您在头文件中声明的默认构造函数添加一个定义。一种方法是将此代码添加到 studentTypeImp.cpp
:
studentType::studentType() {
}
添加默认构造函数,使用命令g++ main.cpp studentTypeImp.cpp
试一试
我正在尝试学习如何在 cpp 中使用 class,其中一项活动让我制作了一个 class header 和一个单独的 cpp 文件以供实施。
这是我的代码:
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;
}
我隐约知道它与我的 header 文件有关,但我不知道从哪里开始。
为您在头文件中声明的默认构造函数添加一个定义。一种方法是将此代码添加到 studentTypeImp.cpp
:
studentType::studentType() {
}
添加默认构造函数,使用命令g++ main.cpp studentTypeImp.cpp
试一试