We help IT Professionals succeed at work.

compile problem: headers and classes

ris asked
Medium Priority
Last Modified: 2010-07-27
I am unable to compile this code and I can't find out why.  What I am trying to do is write a symbol table class.  I am a little out of practice with C++, so I probably have screwed up something very minor and as we all know, those are the hardest errors to find...

Anyway, the error I get is in my .cpp file - it says "SymbolTableEntry is not a class name" when I try to declare anything from the class.  If I comment out all the code for the SymbolTableEntry class, it gives me the same error for the SymbolTable code.  For example, I have this chunk of code in the SymbolTable.cpp file:

  {int i;
  entries=new SymbolTableEntry[TableSize];
  } //end constructor

and when I compile (if I comment out everything above it in the file) I get the error: "SymbolTable is not a class or namespace name" and then the error "declaration terminated incorrectly" at the :: part of the line.

When I was first programming in C++, I got this error because I left out the semi-colon after the class declaration.  The semi-colon is there this time, but I am still unable to compile.

I am using BC++ 5 and here is the code for my header file.


#ifndef SymbolTableHeader
#define SymbolTableHeader true

#include "SymbolTable.cpp"

#define DefaultTableSize 1024

enum EntryType {integer, boolean, floating, function};

class SymbolTableEntry {
  SymbolTableEntry(char* name,int bytes,EntryType t);

  void remove();

  //these functions return the data properties
  char* name();
  int bytes();
  EntryType type();
  bool inuse();
  bool deleted();

  //debugging functions
  void display();

  char ename[];
  int ebytes
  EntryType etype;
  bool einuse;
  bool edeleted;

}; // end class

class SymbolTable {
  SymbolTable(int tsize);

  int insert(SymbolTableEntry NewEntry);
  int query(char* name);
  SymbolTableEntry retrieve(char* name);
  bool remove(char* name);
  bool modify(char* name, SymbolTableEntry NewEntry);

  int size(); //returns the max length of the table
  //debugging functions
  void degub();
  void dump();

  SymbolTableEntry entries[];
  int TableSize;
}; // end class

Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
Okay, you are correct, the error is very minor.  When you use the #include statement, you want to include .h files, not .cpp files as you did on this line:

#ifndef SymbolTableHeader
#define SymbolTableHeader true

#include "SymbolTable.cpp"


You want to reverse the procedure.  You #include the .h file in the .cpp file! :-)

Here is an example:

You have class x:

Here is the sample .h file:

class x
{ x ();
  ~x ();

Sample .cpp file using the x class:

#include "classx.h" // or whatever the name of the class is.
x::x ()

x::~x ()

End sample.

Now, whenever you need to use the class, you include the .h file!

Note that it is legal to #include .cpp files, but I have never seen this in any code ever.  What is happening with the include statement is that the code in the .cpp file is being inserted in place of the #include statement.  So, while the compiler is compiling, it encounters the class constructor before it encounters the class declaration.

I hope I have made that all clear... If you have any questions, please ask before "grading" me.  Thanks!


That helped a lot!  Thanks.

Glad I could help. :-)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.