c++ - C++ 错误“未定义对 'Student::Student 的引用
问题描述
嘿伙计们,我真的可以在这里使用一些帮助。我正在学习 C++ 课程,并且一直在尽力弄清楚整个编程过程。在我的一项作业中,我的程序中出现“未定义的引用”错误。它出现在 main.cpp 文件的第 30 行。使用构造函数 student 创建一个名为 student1 的新学生对象的行。任何帮助找出这个问题的根源将不胜感激。
先感谢您!!
学生.h
#ifndef STUDENT_H_INCLUDED
#define STUDENT_H_INCLUDED
#include <string>
using namespace std;
class Student
{
private:
//instance variables declaration
// declare an int variable called studentID
int studentID;
// declare a string variable called firstName
// declare a string variable called lastName
string firstName;
string lastName;
// declare a string variable called major
string major;
// declare an int variable called gradePoints
int gradePoints;
// declare an int variable called totalCredits
int totalCredits;
public:
//member function declaration
Student(int , string , string , string , int , int );
//function getId() is the accessor for instance variable studentID
int getID();
//function getFullName() is the accessor for both firstName and lastName
//you will need to use string concatenation to return the student full name
string getFullName();
//function getMajor() is the accessor for instance variable major
string getMajor();
//function getGradepoints() is the accessor for instance variable gradePoints
int getGradepoints();
//function getCredits() is the accessor for instance variable totalCredits
int getCredits();
//function changeMajor(string ) is the mutator for instance variable major
//the function declaration is given as below
void changeMajor(string newMajor);
//function changeMajor(string, int, int) is an overloadded mutator for major
//the function declaration is given as below
void changeMajor(string newMajor, int newPoints, int newCredits);
//function toString() is used to print out all instance variables value
string toString();
};
#endif // STUDENT_H_INCLUDED
学生.cpp
#include "Student.h"
#include <iostream>
#include <sstream>
using namespace std;
Student::Student(int id, string fName, string lName, string major, int points, int credits)
{
studentID = id;
// write the segment of codes that assign input parameters to each of the instance variables
firstName=fName;
lastName=lName;
major=major;
gradePoints=points;
totalCredits=credits;
}
int Student::getId()
{
// write a line of code that returns the studentID
return studentID;
}
string Student::getFullName()
{
// write a line of code that returns the full name of the student, includes both first and last name.
stringstream ss;
ss<<firstName<<" "<<lastName;
return ss.str();
}
string Student::getMajor()
{
// write a line of code that returns the student's major
return major;
}
int Student::getGradepoints()
{
// write a line of code that returns the student grade points
return gradePoints;
}
int Student::getCredits()
{
// write a line of code that returns the student total credits
return totalCredits;
}
void Student::changeMajor(string newMajor)
{
// Change the value of the Student’s major variable to the new input’s value.
major=newMajor;
}
void Student::changeMajor(string newMajor, int newPoints, int newCredits)
{
// If newPoints and newCredits are less than or equal to their respective instance variable, update
// the student’s major variable to its new major. Otherwise, print an error message 'Invalid attempt'
major=newMajor;
gradepoints=newPoints;
totalCredits=newCredits;
}
string Student::toString()
{
stringstream ss;
ss<<"==================================="<<endl;
ss<<"Student ID :"<<getID()<<endl;
ss<<"Student Name :"<<getFullName()<<endl;
ss<<"Major :"<<getMajor()<<endl;
ss<<"Num. of Points :"<<getGradepoints()<<endl;
ss<<"Total Credits :"<<getCredits()<<endl;
return ss.str();
}
主文件
#include <iostream>
using namespace std;
#include "Student.h"
int main()
{
//declare variables where you will store inputs from user
int studentID;
string firstName;
string lastName;
string major;
int gradePoints;
int totalCredits;
//prompt the user for inputs studentID, firstName, lastName, major
//gradePoints and totalCredits.
//store the input in the declared variables
cout << "Enter student ID: ";
cin>>studentID;
cout << "Enter first name: ";
cin>>firstName;
cout << "Enter last name: ";
cin>>lastName;
cout << "Enter student major: ";
cin>>major;
cout << "Enter # of Points: ";
cin>>gradePoints;
cout << "Enter # of credits: ";
cin>>totalCredits;
//use the constructor with arguments to create a brand-new Student object
//called student1 using the variable values provided by the user
Student student1(studentID,firstName,lastName,major,gradePoints,totalCredits);
//call the getFullName() function to get the full name of the student.
cout << "\nStudent Name:\t" <<student1.getFullName()<<"\n";
//call the getId() method to get the ID of the student
cout <<"\nStudent ID:\t" << student1.getID() << "\n";
//call the toString() method to get every info. of the student
//show it on screen
cout << student1.toString() << endl;
//Attempt to change the major to 'International Affairs' with 10 points and 500 credits
//by calling changeMajor(String newMajor, int newPoints, int newCredits) function
//This should not succeed. It should print the 'Invalid attempt" message.
student1.changeMajor("International Affairs,10,500");
//call getMajor() method and store the student's old major
//into a variable oldMajor
string oldMajor =student1.getMajor();
//Change just the student’s major to
//'International Affairs' by calling changeMajor(String newMajor) function
student1.changeMajor("International Affairs");
// Print out the following message on screen
// <Student full name> has changed major from <Student_old_major> to <Student_new_major>
cout<<student1.getFullName()<<" has changed major from "<<oldMajor<<" to "<<student1.getMajor()<<endl;
//call toString() function to print student1 info. again on screen
cout<<student1.toString()<<endl;
}
我正在使用 GNU GCC 编译器来编译程序。
解决方案
推荐阅读
- .net-core - .Net Core WinForms 应用程序的 Properties.Settings.Default 输出在哪里?
- javascript - Javascript事件队列任务序列号?
- java - 似乎无法将 Chronometer 转换为 int
- python - 如何获得最重要单词的 TF-IDF 分数?
- java - 在 Java 中将此格式“Tue,14 Jul 2020 03:15:03 +0000 (UTC)”的日期转换为这种格式“2020-02-11 16:05:00”
- python - 图例比轴宽的紧凑布局
- django - 将网站图标放在上传的每张图片上
- excel - VBA宏对具有多个日期的列进行排序,旧日期到新日期
- azure - 在我们的 devops 实例和客户 devops 实例之间分享故事、测试计划和更新
- python - pyTorch 相当于 SciPy.minmise