c++ - Class won't work with .hpp file included in main, but does with .cpp file include in main -
this question has answer here:
i'm having issue classes in ide tells me have undefined reference constructor if include .hpp file in main, not if include .cpp file in main instead. problem in class , across various sites, should .h or .hpp file included in main. here 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 help.
edit: doesn't appear duplicate me, don't have ability @ point understand linked page. if somehow says same thing, it's in language me , still needed help.
you should compile student.cpp , main.cpp together. e.g in linux
g++ main.cpp student.cpp -o a.out
you 1 output named "a.out" links of them together. run a.out.
for windows codeblocks:
Comments
Post a Comment