I took these codes from Deitel's book directly, I tried them at both Linux gcc and Codeblocks on Windows and they both gave errors. Can you tell me why I am getting these errors? Am I doing a mistake by compiling them? Here is the code:
new.cpp:

#include <iostream>
  using std::cout;
  using std::endl;

  #include "GradeBook.h" // include definition of class GradeBook

  // function main begins program execution
  int main()
  {
     // create two GradeBook objects
     GradeBook gradeBook1( "CS101 Introduction to C++ Programming" );
     GradeBook gradeBook2( "CS102 Data Structures in C++" );

     // display initial value of courseName for each GradeBook
     cout << "gradeBook1 created for course: " << gradeBook1.getCourseName()
        << "\ngradeBook2 created for course: " << gradeBook2.getCourseName()
        << endl;
     return 0; // indicate successful termination
  } // end main

GradeBook.h :

#include <string> // class GradeBook uses C++ standard string class
  using std::string;

  // GradeBook class definition
  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>
  using std::cout;
  using std::endl;

  #include "GradeBook.h" // include definition of class GradeBook

  // constructor initializes courseName with string supplied as argument
  GradeBook::GradeBook( string name )
  {
     setCourseName( name ); // call set function to initialize courseName
  } // end GradeBook constructor

  // function to set the course name
  void GradeBook::setCourseName( string name )
  {
     courseName = name; // store the course name in the object
  } // end function setCourseName

  // function to get the course name
  string GradeBook::getCourseName()
  {
     return courseName; // return object's courseName
  } // end function getCourseName

  // display a welcome message to the GradeBook user
  void GradeBook::displayMessage()
  {
     // call getCourseName to get the courseName
     cout << "Welcome to the grade book for\n" << getCourseName()
        << "!" << endl;
  } // end function displayMessage

You are including gradebook.h from two files, so the compiler will parse it twice. By doing so it will throw errors because of something like 'XXX already defined in blabla.o'.

To prevent this, you should always 'guard' your header files.
So GradeBook.h should be:

#ifndef GRADEBOOK_H_INCLUDED
#define GRADEBOOK_H_INCLUDED

// All of the GradeBook.h content goes here.

#endif // GRADEBOOK_H_INCLUDED

Next time you post a question, please also post what errors you are getting. I foun this problem by reading your code, but it's possible that there are more errors.

Thank you very much for the respond but I'm still getting errors. Here is the error in Linux gcc:: In function `main':
newest.cpp: (.text+0x1b5): undefined reference to `GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
newest.cpp: (.text+0x228): undefined reference to `GradeBook::GradeBook(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
newest.cpp: (.text+0x260): undefined reference to `GradeBook::getCourseName()'
newest.cpp: (.text+0x289): undefined reference to `GradeBook::getCourseName()'
collect2: ld returned 1 exit status


and in windows codeblocks:
C:\Users\pc\Desktop\workspace\newest.o:newest.cpp: (.text+0x342)||undefined reference to `GradeBook::getCourseName()'|
C:\Users\pc\Desktop\workspace\newest.o:newest.cpp: (.text+0x31a)||undefined reference to `GradeBook::getCourseName()'|
C:\Users\pc\Desktop\workspace\newest.o:newest.cpp: (.text+0x283)||undefined reference to `GradeBook::GradeBook(std::string)'|
C:\Users\pc\Desktop\workspace\newest.o:newest.cpp: (.text+0x1aa)||undefined reference to `GradeBook::GradeBook(std::string)'|

doesn't any of you know this type of error or am I too blind to see the solution?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.