Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

compile problem: headers and classes

Posted on 1998-09-03
4
Medium Priority
?
147 Views
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:

SymbolTable::SymbolTable()
  {int i;
  TableSize=DefaultTableSize;
  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.

SymbolTable.h:

#ifndef SymbolTableHeader
#define SymbolTableHeader true

#include "SymbolTable.cpp"

#define DefaultTableSize 1024

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

class SymbolTableEntry {
public:
  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();

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

}; // end class


class SymbolTable {
public:
  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();

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

#endif
0
Comment
Question by:ris
  • 3
4 Comments
 
LVL 6

Accepted Solution

by:
thresher_shark earned 800 total points
ID: 1172017
Answer coming...
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1172018
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!
0
 
LVL 1

Author Comment

by:ris
ID: 1172019
That helped a lot!  Thanks.

0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1172020
Glad I could help. :-)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question