Undefined reference to ... | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Undefined reference to ...

I think I declared and implemented the class 'GradeBook' in 'GradeBook.h' and 'GradeBook.cpp' files respectively. I also put the files in the same folder with the main.cpp file. However, I get the the errors 1) 'undefined reference to 'GradeBook::GradeBook(std::string)'. 2)'undefined reference to 'GradeBook::displayMessage()' where displayMessage is a public member function. My Header and Source Files of the class are as follows Header File: #include <iostream> #include <string> using namespace std; class GradeBook { public: GradeBook(string name); void setCourseName(string name); string getCourseName(); void displayMessage(); private: string courseName; }; Source Code: #include <iostream> #include <string> #include "GradeBook.h" using namespace std; GradeBook::GradeBook(string name) { courseName = name; } GradeBook::setCourseName(string name) { if (name.length() <= 25) courseName = name; if (name.length() > 25) { courseName = name.substr(0,25); cout << "Name" << name << "exceeds 25 character.\n" << "Limiting course name to first 25 characters.\n" } } GradeBook::getCourseName() { return courseName; } GradeBook::displayMessage() { cout << "Welcome to grade book for\n" << getCourseName() << endl; } Test Code: #include <iostream> #include <string> #include "GradeBook.h" using namespace std; int main() { string name = "Default Name"; GradeBook gradeBook1(name); gradeBook1.displayMessage(); } I use CodeBlocks IDE to compile the code. Is there anyone who knows what is my mistake.!

5th Nov 2017, 12:23 PM
Adem şahin
Adem şahin - avatar
4 Answers
+ 14
I was referring to your class method definition outside the class, not the method declaration. You missed out the return types.
5th Nov 2017, 12:33 PM
Hatsy Rei
Hatsy Rei - avatar
+ 12
Function return type not defined during class method definition.
5th Nov 2017, 12:28 PM
Hatsy Rei
Hatsy Rei - avatar
0
It is constructor, not function
5th Nov 2017, 12:30 PM
Adem şahin
Adem şahin - avatar
0
Thanks, I corrected it but the error is still there
5th Nov 2017, 12:39 PM
Adem şahin
Adem şahin - avatar