Question

I'm a beginner with C++. I wrote the following:

// GradeBook.h
#include <iostream>
#include <string> 
using namespace std;

class GradeBook { 
public:
    GradeBook(string); // constructor that initializes courseName
    void setCourseName(string); // function that sets the course name
    string getCourseName(); // function that gets the course name
    void displayMessage(); // function that displays a welcome message 
private:
    string courseName; // course name for this GradeBook
};

// GradeBook.cpp
#include <iostream>
#include "GradeBook.h"
using namespace std;

GradeBook::GradeBook(string name)
{
    setCourseName(name);
} 

void GradeBook::setCourseName(string name)
{
    courseName = name;
}

string GradeBook::getCourseName()
{
    return courseName;
}

void GradeBook::displayMessage()
{
    cout << "Welcome to the grade book for\n" << getCourseName() << "!" << endl;
}

// main.cpp
#include <iostream>
#include "GradeBook.h"
using namespace std;

int main()
{
    GradeBook gradeBook1("CS101 Introduction to C++ Programming");
    GradeBook gradeBook2("CS102 Data Structures in C++");

    cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
         << "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
         << endl;
}

I am using KDevelop 4.4.1, then I proceed to execute my main.cpp and I got:

/home/brallan/projects/Hola/build> make
Linking CXX executable hola
CMakeFiles/hola.dir/main.o: In function main':
/home/brallan/projects/Hola/main.cpp:8: undefined reference to GradeBook::GradeBook(std::string)'
/home/brallan/projects/Hola/main.cpp:9: undefined reference to GradeBook::GradeBook(std::string)'
/home/brallan/projects/Hola/main.cpp:12: undefined reference to GradeBook::getCourseName()'
/home/brallan/projects/Hola/main.cpp:11: undefined reference to GradeBook::getCourseName()'
collect2: error: ld returned 1 exit status
make[2]: [hola] Error 1
make[1]: [CMakeFiles/hola.dir/all] Error 2
make: [all] Error 2
Failed

If I run the same code from Eclipse Juno CDT, it return me:

gradeBook1 created for course: CS101 Introduction to C++ Programming
gradeBook2 created for course: CS102 Data Structures in C++

Can anyone help me to run it from KDevelop?

UPDATE: Based on the comments, KDevelop isn't compiling other files in the project :s I guess this is the problem to be solved.

Was it helpful?

Solution 3

In the project folder, there is a file called CMakeList.txt and on it are the files that are part of the project. I tried to add the file GradeBook.cpp to add_executable line, but still did not work. However, when I replaced the file names in lower case, and turn modify the line that I described, everything worked properly. I'm not sure what is the mistake if the file name has no upper or similarly if I add it to this list exactly as it is called.

Then, I renamed files gradebook.h and gradebook.cpp and added gradebook.cpp to add_executable line.

OTHER TIPS

First, add the line #error (or any other syntax error) to the end of GradeBook.cpp. Ensure you get a compilation error for that line when you try to build it. If not, check spelling and capitalization of the file reference from the project or makefile.

If you do get a syntax error, or if you don't but you can't figure out why the file isn't being referenced, try this next: Remove the #error from GradeBook.cpp, and add #include "GradeBook.cpp" to the end of main.cpp. This serves two purposes: It get you going (should now be able to build and run) and it helps narrow the problem (if it works, you know the problem is with referencing GradeGook.cpp, rather than with its contents).

It seems you are not compiling GradeBook.cpp

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top