Results 1 to 6 of 6

Thread: C++ code/compile help

  1. #1
    Registered User ToeMissile's Avatar
    Join Date
    Dec 2012
    Posts
    443
    Tuconots
    1

    C++ code/compile help

    So having an issue that my tired noob eyes/brain can't work out. Read my notes/book/looked online to no avail. I know it's something simple that I'm just not seeing.. anyhow, i'll only post one set of .h/.cpp as the others are basically the same. The main.cpp is provided by the instructor.

    When I compile, I'm getting undefined reference errors for any call to/for a function from the class
    Spoiler: 
    [code]
    In function `TestStudent()':
    main.cpp.text+0x24c): undefined reference to `CStudent::CStudent()'
    main.cpp.text+0x257): undefined reference to `CStudent::GetGPA()'
    main.cpp.text+0x2ae): undefined reference to `CStudent::CStudent(CStudent const&)'
    main.cpp.text+0x2b9): undefined reference to `CStudent::GetGPA()'
    main.cpp.text+0x328): undefined reference to `CStudent::CStudent(double)'
    [code]


    teacher.h
    Code:
    #ifndef TEACHER_H
    #define TEACHER_H
    
    class CTeacher
    {
        double m_Salary;
    
    public:
        CTeacher();
        CTeacher(const CTeacher& input);
        CTeacher(double salary);
    
        double GetSalary();
        void SetSalary(double input);
    };
    
    #endif
    teacher.cpp
    Code:
    #include "teacher.h"
    
    
    double CTeacher::GetSalary()
    {
        return m_Salary;
    }
    
    void CTeacher::SetSalary(double input)
    {
        m_Salary = input;
    }

    main.cpp
    Code:
    #include <iostream>
    #include "student.h"
    #include "teacher.h"
    #include "classroom.h"
    
    using namespace std;
    
    
    bool TestStudent();
    bool TestTeacher();
    bool TestClassroom();
    
    // ==== main ========================================================
    //
    //===================================================================
    int main()
    {
        cout << "====================================== \n";
        cout << "Testing Student \n";
        cout << "====================================== \n";
        if(!TestStudent())
        {
            cout << endl << endl;
            cout << "Student test failed! Fix those bugs! \n";
            return 0;
        }
    
        cout << endl << endl;
        cout << "====================================== \n";
        cout << "Testing Teacher \n";
        cout << "====================================== \n";
        if(!TestTeacher())
        {
            cout << endl << endl;
            cout << "Teacher test failed! Fix those bugs! \n";
            return 0;
        }
        cout << endl << endl;
        cout << "====================================== \n";
        cout << "Testing Classroom \n";
        cout << "====================================== \n";
        if(!TestClassroom())
        {
            cout << endl << endl;
            cout << "Classroom test failed! Fix those bugs! \n";
            return 0;
        }
        cout << endl << endl;
        return 0;
    }
    
    
    //==== TestStudent ==================================================
    // This function tests the default constructor, the copy constructor,
    // the conversion constructor and get gpa method on the student class.
    // Input:
    //   N/A
    // Output:
    //   Returns whether the tests on the student passed
    //===================================================================
    
    bool TestStudent()
    {
        cout << "Inside test student \n";
        cout << "Testing default constructor: \n";
        CStudent defaultStudent;
        if(defaultStudent.GetGPA() != 0)
        {
            cout << "Failed to initialize GPA! \n";
            return false;
        }
        cout << "Testing copy constructor: \n"; 
        CStudent copyStudent(defaultStudent);
        if(defaultStudent.GetGPA() != copyStudent.GetGPA())
        {
            cout << "Failed to copy GPA! \n";
            return false;
        }
        cout << "Testing conversion constructor: \n";
        double GPA = 4;
        CStudent conversionConstructor(GPA); 
        if(GPA != conversionConstructor.GetGPA())
        {
            cout << "Failed to convert GPA! \n";
            return false;
        }
        cout << "Returning from test student \n";
        return true;
    }
    
    
    //==== TestTeacher ==================================================
    // This function tests the default constructor, the copy constructor,
    // the conversion constructor and get salary method on the teacher class.
    //
    // Input:
    //   N/A
    // Output:
    //   Returns whether the tests on the teacher passed
    //===================================================================
    
    bool TestTeacher()
    {
        cout << "Inside test teacher \n"; 
        cout << "Testing default constructor: \n";
        CTeacher defaultTeacher;
        if(defaultTeacher.GetSalary() != 0)
        {
            cout << "Failed to properly initialize salary! \n";
            return false;
        }
        cout << "Testing copy constructor: \n"; 
        CTeacher copyTeacher(defaultTeacher);
        if(copyTeacher.GetSalary() != defaultTeacher.GetSalary())
        {
            cout << "Failed to copy salary! \n";
            return false;
        }
        cout << "Testing conversion constructor: \n";
        double salary = 40000;
        CTeacher conversionTeacher(salary); 
        if(salary != conversionTeacher.GetSalary())
        {
            cout << "Failed to convert salary! \n";
            return false;
        }
        cout << "Returning from test teacher \n";
        return true;
    } // end of test teacher
    
    
    //==== TestClassroom ================================================
    // This function tests the default constructor, the copy constructor,
    // the conversion constructor and get salary method on the  
    // classroom class.
    //
    // Input:
    //   N/A
    // Output:
    //   Returns whether the tests on the classroom passed
    //
    //===================================================================
    
    bool TestClassroom()
    {
    
        cout << "Inside test classroom \n"; 
        cout << "Testing default constructor: \n";
        CClassroom defaultClassroom;
        if(defaultClassroom.GetClassSize() != 0)
        {
            cout << "Failed to initialize class size! \n";
            return false;
        }
        cout << "Testing copy constructor: \n"; 
        CClassroom copyClassroom(defaultClassroom);
        if(defaultClassroom.GetClassSize() != copyClassroom.GetClassSize())
        {
            cout << "Failed to copy class size! \n";
            return false;
        }
        cout << "Testing conversion constructor: \n";
        int classSize = 30;
        CClassroom conversionClassroom(classSize); 
        if(conversionClassroom.GetClassSize() != classSize)
        {
            cout << "Failed to convert class size! \n";
            return false;
        }
        cout << "Returning from test classroom \n";
        return true;
    }

  2. #2
    Registered User
    Join Date
    Dec 2012
    Posts
    30
    Tuconots
    1
    Does student.h have all the functions from your error? Do you have a student.cpp? Are the functions in question defined in student.cpp? Are you sure it's getting compiled in?

  3. #3
    Iksar Boots Alkorin's Avatar
    Join Date
    Dec 2012
    Location
    Ottawa, Canada
    Posts
    2,137
    Tuconots
    13
    Yeah, we'd need to see the declaration for the student part, not the teacher part, unless that is also giving you an error. Looks like you're not defining CStudent properly, or (and I'm guessing here, because you didn't include the code) your #ifndef at the top of student.h is actually an #ifdef and it's never getting included at all.

  4. #4
    Registered User ToeMissile's Avatar
    Join Date
    Dec 2012
    Posts
    443
    Tuconots
    1
    So here's student.h/.cpp

    student.h
    Code:
    #ifndef STUDENT_H
    #define STUDENT_H
    
    class CStudent
    {
        double m_GPA;
    
    public:
        CStudent();
        CStudent(const CStudent& input);
        CStudent(double GPA);
    
        double GetGPA();
        void SetGPA(double input);
    };
    
    
    #endif
    student.cpp
    Code:
    #include "student.h"
    
    double CStudent::GetGPA()
    {
        return m_GPA;
    }
    
    void CStudent::SetGPA(double input)
    {
        m_GPA = input;
    }

  5. #5
    Registered User Kharza-kzad's Avatar
    Join Date
    Dec 2012
    Posts
    1,081
    Tuconots
    6
    Not sure why it can't find the gets and sets, but the constructors aren't written. I think you can just let the compiler make the default CStudent(); one. You'll need to write the ones that take a reference and a double.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    85
    Tuconots
    0
    You need to implement the constructors that you defined in you header files. Until you do that, the compiler won't even be able to create the obj to link, so you'll get errors for every symbol that it can't resolve.

    So, in classroom.cpp, you'll need to implement the two constructors, in student.cpp there should be 3 constructors, and for teacher.cpp there are 3 constructors. Now, you COULD implement them in the header files as well, but probably just best that you get into the habit of keeping all the implementations in your cpp file. As an example, in your student.h you could have CStudent() { }; vs CStudent::CStudent() { } in the cpp.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •