1

Hey Guys I could really use some help here. I am in a intro to C++ class, and have been trying my best to figure out this whole programming thing out. In one of my assignments i am getting an "undefined reference to" error in my program. It is coming up on line 30 of the main.cpp file. The line that creates a new student object called student1 using the constructor student. Any help figuring out the root of this problem would be greatly appreciated.

Thank you in advance!!

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;

}

I am using a GNU GCC Compiler to compile the program.

Sebastian
  • 1,834
  • 2
  • 10
  • 22
  • I don't think so? Did i not properly compile the files together? The error I am getting is that on line 30 of the main.cpp file, It says that it has a "undefined reference to 'Student::Student(int,Etc.), and then when i call the object in the lines thereafter they also have undefined reference errors. I am sorry if i'm blatently missing out on something. – Kodi Dewitt Apr 05 '20 at 06:50
  • *Did i not properly compile the files together?* How can we know it, you showed nothing. That article explains all possible cases. – 273K Apr 05 '20 at 06:53
  • I am sorry, can you please expand on how I showed nothing? I am not quite sure what you are referencing I need to show? I was under the impression that I had showed all of the code that I could? – Kodi Dewitt Apr 05 '20 at 06:57
  • Nothing regarding how you compile, properly or improperly. – 273K Apr 05 '20 at 07:05
  • I can't seem to figure out what you mean by how I am compiling. I am reading through the links that you have sent, and I can not for the life of me figure out what this whole compiling thing that you are talking about is. – Kodi Dewitt Apr 05 '20 at 07:19
  • @KodiDewitt How do you compile the program? There are multiple typical methods: 1. Invoking the compiler directly, e.g. by typing `g++ ...` in a terminal. 2. Using a build system such as `make` or `cmake`. 3. Letting the IDE do the build. In each case you should be able to describe what *exactly* you are doing, e.g. what options you pass to `g++`, how the `Makefile` looks or what IDE you use, etc. – walnut Apr 05 '20 at 07:34
  • 1
    When I try to compile your program, I get an error in Student.cpp 17:20 (no matching declaration) and Student.cpp 54:5 (variable not declared in this scope). It looks like you have problems with case sensitivity in these places. – Shaavin Apr 05 '20 at 07:38
  • 2
    @Shaavin It seems pretty clear that OP is not compiling the `Student.cpp` file at all. If they did, they would see these errors as well. – walnut Apr 05 '20 at 07:39
  • You're right. My bad. – Shaavin Apr 05 '20 at 07:40
  • 1
    @walnut I am using Code Blocks as the IDE, and I am letting it do the build. How can I make sure that It compiles the student.cpp file? – Kodi Dewitt Apr 05 '20 at 07:41
  • I dislike the omnibus What is an undefined reference/unresolved external symbol error and how do I fix it? answer. If the solution isn't in the first few answers it looks pretty much like a big you to the asker. If the question does not specify enough information for you to drop a comment pointing to which answer applies we should probably close as "Needs more Details." – user4581301 Apr 05 '20 at 07:44
  • @walnut I realized my mistake, You all were correct, I was not compiling the student.cpp file, and I needed to go into the file's property's and turn on the debug, and release features. Thank you for the help. – Kodi Dewitt Apr 05 '20 at 07:57
  • I voted to reopen since on second thought is is not good to vote when I don't know about Code::Blocks. I was not aware that this would cause the question to be immediately reopened, even though other users had close-voted. The original duplicate list can still be seen in the [revision list](https://stackoverflow.com/posts/61038932/revisions). Going by OP's comment, the question should be closed as duplicate of [Code::Blocks - how to compile multiple source files](https://stackoverflow.com/questions/5971206/codeblocks-how-to-compile-multiple-source-files), I guess, but I cannot vote again. – walnut Apr 05 '20 at 07:59
  • You need to include in the question **everything your compiler outputs**, verbatim. Do not give executive summaries. – n. m. could be an AI Apr 05 '20 at 08:07
  • Does this answer your question? [Code::Blocks - how to compile multiple source files](https://stackoverflow.com/questions/5971206/codeblocks-how-to-compile-multiple-source-files) – Azeem Apr 05 '20 at 09:52

1 Answers1

0

I think maybe here's the solution:

"This task tells g++ to take the active file (${file}), compile it, and create an executable file in the current directory (${fileDirname}) with the same name as the active file but without an extension (${fileBasenameNoExtension}), resulting in helloworld for our example."

F. Z
  • 21
  • 2
  • 1
    Thank you for contributing. You are referring to "this task" without describing, what "this task" is. The error is a specific compile error. As came out in the comments the problem are the Code::Blocks settings. Are you referring to a solution for it? – Sebastian Sep 17 '22 at 10:40