Thread: Separating class implementation

  1. #1
    Registered User
    Join Date
    Feb 2009

    Separating class implementation

    I was reading Deitels' book, and in one of the chapters the reader is supposed to learn how to implement classes by separating the interface in a header file, and the code itself on a source code file.

    They use an example where you define a class "GradeBook" that will create a GradeBook for a certain class, and has member functions to set the class name, read the class name and display a "welcome" message.

    The problem is I can't even compile the function code... It always returns an error. First it was returning some errors about the class implementation, and now it's returning a linker error...

    Anyone knows what's wrong with the code?

    #include <iostream>
    #include <string>
    #include "gradebook.h"
    using std::cout;
    using std::string;
    GradeBook::GradeBook(string name){
    void GradeBook::setCourseName(string name){
         courseName = name;
    string GradeBook::getCourseName(){
           return courseName;
    void GradeBook::displayMessage(){ 
         cout << "Welcome to GradeBook for: " << getCourseName();
    #include <string>
    using std::string;
    class GradeBook
                 string getCourseName();
                 void setCourseName(string);
                 void displayMessage();
                  string courseName;
    I'm not including the main code as the problem is only the class.

    Any help appreciated.
    Last edited by Litz; 03-06-2009 at 05:19 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Linker error usually means "you didn't put all the files in the same project". More specifics given get more specific answers.

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    CA, USA
    Post the error you are getting; it will help others identify the problem more easily.

    I would suspect that you're getting multiple definition errors. You have included gradebook.h in your gradebook.cpp file but you have also included gradebook.h in your main file, in order to use the class. When you compile your two source files, the preprocessor includes two copies of the gradebook class definition, one in main.cpp and one in gradebook.cpp. To fix this, put these inclusion guards in the header file:

    #ifndef GRADEBOOK_H
    #define GRADEBOOK_H
    // code goes in between
    If you do this, the definition will only get included one time. You must do this on virtually all header files.

    Also, I would avoid putting any using statements in a header file. Putting them in a source file is fine. Someone else can probably explain that to you.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Quote Originally Posted by Litz View Post
    The problem is I can't even compile the function code... It always returns an error. First it was returning some errors about the class implementation, and now it's returning a linker error...

    Anyone knows what's wrong with the code?
    On a quick look, the code you've supplied looks alright (albeit not doing things in a particularly good manner, but that's subjective).

    Your words here also imply you're trying various things (eg hacking your code, tweaking compiler settings) and it's quite possible the code you've shown isn't actually related to the problems you're seeing. If you're getting compiler errors, there is no point in trying to link.

    In any event, it would be helpful if you posted information about your development environment (which compiler, compiler settings, IDE options set, etc). And, most importantly, what error messages you're seeing.

    It would also help if you provided the "main code", as there are things you could be doing in there that would cause compiler errors. For example, multiple inclusions of "gradebook.h" and "gradebook.cpp" within your main code would upset the compiler.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Feb 2009
    I created a project now and added them all to the same project, compiled and it worked.

    I also removed the using statement on the header file and added the code to stop multiple definition of the same class. Even though it worked before I did that, I'm sure there's good reasons to do it so I'll look around for it.

    I also created a couple more classes to check if I got it this time, and everything's working fine, so the problem was probably just adding them all to same project.

    Thanks for the help everyone.

  6. #6
    Join Date
    Oct 2007
    Inside my computer
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Implementation of a template class functions
    By 39ster in forum C++ Programming
    Replies: 3
    Last Post: 01-08-2008, 03:36 AM
  3. Implementation of set class in C++
    By hay_man in forum C++ Programming
    Replies: 10
    Last Post: 09-21-2006, 03:33 PM
  4. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  5. Hide class implementation from driver?
    By wbeasl in forum C++ Programming
    Replies: 5
    Last Post: 09-13-2004, 05:38 PM