提问人:Kodi Dewitt 提问时间:4/5/2020 最后编辑:SebastianKodi Dewitt 更新时间:9/17/2022 访问量:1284
C++ 错误“对”学生::学生“的未发现引用
C++ Error "Undefinded reference to 'Student::Student
问:
嘿,伙计们,我真的可以在这里使用一些帮助。我正在学习 C++ 课程,并且一直在尽最大努力弄清楚整个编程问题。在我的一项作业中,我在程序中收到“未定义引用”错误。它出现在 main.cpp 文件的第 30 行。使用构造函数 student 创建名为 student1 的新学生对象的行。任何帮助找出这个问题的根源将不胜感激。
先谢谢你!!
Student.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();
}
main.cpp
#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 编译器来编译程序。
答:
0赞
F. Z
9/17/2022
#1
我想也许这就是解决方案:
“此任务告诉 g++ 获取活动文件 (${file}),对其进行编译,并在当前目录 (${fileDirname}) 中创建一个可执行文件,该文件与活动文件同名,但没有扩展名 (${fileBasenameNoExtension}),从而为我们的示例生成 helloworld。”
评论
1赞
Sebastian
9/17/2022
谢谢你的贡献。你指的是“这个任务”,但没有描述“这个任务”是什么。该错误是特定的编译错误。正如评论中指出的那样,问题在于Code::Blocks设置。你指的是它的解决方案吗?
评论
Student.cpp