[try Beta version]
Not logged in

 
Problem with header file

Nov 19, 2009 at 5:12am
I am trying to compile the following code to calculate the average of grades but I get the error that GradeBook.h not found. I am primarily a student of java and I have been able to work similar codes in java without any hitch. Will be grateful for clarification as to what I should have done to avoid the error.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <string> 
   using std::string;

 class GradeBook
   {
  public:
     GradeBook( string ); 
     void setCourseName( string ); 
     string getCourseName(); 
     void displayMessage(); 
     void determineClassAverage(); 
  private:
     string courseName; 
  }; 
//=============================
#include <iostream>
   using std::cout;
   using std::cin;
   using std::endl;
 
   #include "GradeBook.h" 
  
  GradeBook::GradeBook( string name )
  {
     setCourseName( name ); 
  } 
void GradeBook::setCourseName( string name )
  {
     if ( name.length() <= 25 ) 
        courseName = name; 
     else 
     { 
        courseName = name.substr( 0, 25 ); 
        cout << "Name \"" << name << "\" exceeds maximum length (25).\n"
           << "Limiting courseName to first 25 characters.\n" << endl;
     } 
  } 
  
  string GradeBook::getCourseName()
  {
     return courseName;
  } 

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

  
  void GradeBook::determineClassAverage()
  {
     int total; 
     int gradeCounter; 
     int grade; 
     int average; 

     
     total = 0; 
     gradeCounter = 1; 

     
     while ( gradeCounter <= 10 ) 
     {
        cout << "Enter grade: "; 
        cin >> grade; 
        total = total + grade; 
        gradeCounter = gradeCounter + 1; 
     } 
     
     average = total / 10; 

     
     cout << "\nTotal of all 10 grades is " << total << endl;
     cout << "Class average is " << average << endl;
  } 
//======================
#include "GradeBook.h" 
 
   int main()
   {
      
      
      GradeBook myGradeBook( "CS101 C++ Programming" );

     myGradeBook.displayMessage(); 
     myGradeBook.determineClassAverage(); 
     return 0; 
  } 





Nov 19, 2009 at 5:14am
Make sure GradeBook.h is in the same directory as your other files (the .cpp).
Nov 19, 2009 at 8:04am

Let me explain what I am doing.
1. I am using MS vis C++6.
2. I make a project directory and from File -> New, I select the 'C/C++ Header File' and add GradeBook file. I can find the GradeBook.h file in the directory.
3. Then, from File -> New, I select C++ Source File and add the remaining two codes given in post 1.
4. Then I compile and get the error ...C2011 - class type redefinition.
Nov 19, 2009 at 12:13pm
Rather than paraphrasing the error, it may help if you post the actual error in entirety.

The problem is described in the error message.
Nov 19, 2009 at 2:33pm
Many many thanks everyone!! The problem was solved by placing 'header guard' and 'end guard' in the header file.
Topic archived. No new replies allowed.