0

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.

Chu
  • 468
  • 1
  • 5
  • 21
  • 1
    It seems that your IDE isn't compiling `GradeBook.cpp`. – Luchian Grigore Dec 03 '12 at 23:10
  • Is your code formatted as shown? For instance, do you really have a using namespace on the same line as an #include? It sounds like you may have an error with finding Grade.cpp. When I put everything in one file and make minor adjustments (like putting the namespace on its own line and adding a return at the end of main) it works, so it looks like it's an issue with finding the files. – RonaldBarzell Dec 03 '12 at 23:52
  • No, I've the using namespace in a new line. – Chu Dec 04 '12 at 20:43
  • Have you included GradeBook.cpp in the KDevelop project? When you rebuild the project, do you see compiler messages pertaining to that file? – n. m. could be an AI Dec 06 '12 at 04:39
  • When I created my C++ project, I was making every file using KDevelop at the same folder... – Chu Dec 06 '12 at 04:54
  • I fervently recommend against 'using namespace' commands in a header. I've been on big projects where that happened and scope resolution can become messy for source files that include those headers. We were finding source files where the order of include statements mattered because statements like that in one header would wreak havoc on headers included later. – John Dec 06 '12 at 16:37

3 Answers3

5

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 gets 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).

Edward Brey
  • 40,302
  • 20
  • 199
  • 253
  • First I wrote a line with errors at GradeBook.cpp, and later compile main.cpp without shows me the errors. Finally, I deleted the errors and put #include "GradeBook.cpp" at main.cpp obtaining the result I wanted. But it is assumed that the way I got from the beginning is correct? – Chu Dec 06 '12 at 05:33
  • 1
    That you mention "compile main.cpp" is a clue. Typically, do don't compile individual .cpp files, but rather the project as a whole. If for some reason, you could only tell the compiler to compile a single file (main.cpp in this case), using a #include to pull in GradeBook.cpp would be a correct approach. But this isn't the design of C++. You've proved to yourself that the compiler is only using main.cpp as a translation unit. So now focus on ensuring you're compiling the whole project, not an individual file, and that main.cpp and GradeBook.cpp are both in the project with identical settings. – Edward Brey Dec 06 '12 at 12:15
1

It seems you are not compiling GradeBook.cpp

  • You are right. But, how can I compile all the files in my project using KDevelop? – Chu Dec 06 '12 at 22:36
1

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.

Chu
  • 468
  • 1
  • 5
  • 21