-4

I'm having an issue with classes in that my IDE tells me that I have an undefined reference to my constructor if I include my .hpp file in main, but not if I include my .cpp file in main instead. My problem is that in class and across various sites, it should be the .h or .hpp file included in the main. Here are my files:

Student.hpp

#ifndef STUDENT_HPP
#define STUDENT_HPP

class Student
{
private:
    std::string lastName;
    double gpa;

public:
    Student();
    Student(std::string LastName, double Gpa);

    void setLastName(std::string LastName);
    std::string getLastName();

    void setGpa(double Gpa);
    double getGpa();
};

#endif //STUDENT_HPP

Student.cpp

#include <iostream>
#include "Student.hpp"
using namespace std;


Student::Student()
{
    lastName = "unknown";
    gpa = 0.0;
}
Student::Student(string LastName, double Gpa)
{
    lastName = LastName;
    gpa = Gpa;
}

void Student::setLastName(string LastName)
{
    lastName = LastName;
}

string Student::getLastName()
{
    return lastName;
}

void Student::setGpa(double Gpa)
{
     gpa = Gpa;
}

double Student::getGpa()
{
    return gpa;
}

main.cpp

#include <iostream>
#include "Student.hpp"

using namespace std;


int main()
{
    Student liz = Student();
    cout << liz.getLastName() << endl;



    return 0;
}

Thanks for your help.

Edit: This doesn't appear to be a duplicate to me, I don't even have the ability at this point to really understand the linked page. If it somehow says the same thing, it's in another language to me and I still needed the help.

  • 1
    Whatever you are doing, it is only compiling **main.cpp**; you need to compile **Student.cpp** as well and link them together. – Justin Sep 11 '17 at 21:45
  • Definitely not a duplicate of that. I have compiled Student.cpp; can you be more specific about how to "link them together"? – ULTIMATEDEMISE2 Sep 11 '17 at 21:47
  • 1
    @ULTIMATEDEMISE2 How are you compiling your code? – melpomene Sep 11 '17 at 21:52
  • 4
    @ULTIMATEDEMISE2 _"Definitely not a duplicate of that"_ It definitely is. _"because look at them both."_ Just including the headers isn't enough finally. – user0042 Sep 11 '17 at 21:53
  • @user0042 maybe you could explain how they're the same, to the untrained eye (mine)...there are zero similarities. – ULTIMATEDEMISE2 Sep 11 '17 at 22:00
  • @melpomene Through codeblocks. I'm essentially new to coding; is the gist of what I'm being told that this method really only works through command line? – ULTIMATEDEMISE2 Sep 11 '17 at 22:01
  • 4
    @ULTIMATEDEMISE2 No, it just means your project is set up wrong. – melpomene Sep 11 '17 at 22:05
  • I don't like omnibus answers like What is an undefined reference/unresolved external symbol error and how do I fix it?, but it does cover this problem [in answer number 3.](https://stackoverflow.com/a/12574400/4581301) – user4581301 Sep 11 '17 at 22:15
  • Ohhhh, does this method only work in a "Project"? I haven't ever used that option, or been told to...yikes. – ULTIMATEDEMISE2 Sep 12 '17 at 18:28

1 Answers1

4

You should compile Student.cpp and main.cpp together. e.g in linux

g++ main.cpp Student.cpp -o a.out

You will get only one output named "a.out" that links all of them together. Then run the a.out.

For windows codeblocks:

code::blocks - how to compile multiple file projects

https://www.youtube.com/watch?v=oXaC8t-gPVY

ssovukluk
  • 438
  • 3
  • 14