Ошибка C ++ "Неопределенная ссылка на 'Student :: Student - PullRequest
1 голос
/ 05 апреля 2020

Привет, ребята, я действительно мог бы здесь помочь Я нахожусь в введении в класс C ++, и изо всех сил стараюсь выяснить всю эту вещь программирования. В одном из моих заданий я получаю ошибку «неопределенная ссылка на» в моей программе. Он появляется в строке 30 основного файла. cpp. Строка, которая создает новый объект студента с именем student1, используя конструктор student. Любая помощь в выяснении root этой проблемы будет принята с благодарностью.

Заранее спасибо !!

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

Student. 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 G CC для компиляции программы.

...