CS 410 Programming Assignment 3: Clarifications, Corrections, and Hints

Fall 2006 [Bono]

[10/28/04 CMB] FAQ: I get compile errors when I call SymTab<..>::dump

a student wrote:
Hi, Professor,

     Is this the right way to dump a symbol table?

void Semant::analyzeProg(){

 ast->addDeclr(classSymTab);
 classSymTab->dump(cout);
}

When I compile the above code, I got the following error:

g++ -g -Wall -Wno-unused    -c -o semant.o semant.cc
SymTab.i: In method `void SymTab<Class>::dumpHelper(ostream &) const':
SymTab.i:74:   instantiated from `SymTab<Class>::dump(ostream &) const'
semant.cc:95:   instantiated from here
SymTab.i:55: no match for `ostream & << Class &'
/usr/usc/gnu/gcc/2.95.2/lib/gcc-lib/sparc-sun-solaris2.6/2.95.2/../../../../include/g++-3/iostream.h:77: candidates are: class ostream & ostream::operator <<(char)
/usr/usc/gnu/gcc/2.95.2/lib/gcc-lib/sparc-sun-solaris2.6/2.95.2/../../../../include/g++-3/iostream.h:78:                 class ostream & ostream::operator <<(unsigned char)
[... lots more deleted...]
gmake: *** [semant.o] Error 1
Here are the relevant comments from SymTab.h:

*  symtab->dump(cout); *
*       Mostly for debugging purposes; prints out to the ostream
*       given all the entries in the symbol table, from the innermost
*       scope to the outermost scope, denoting scope boundaries.  It
*       prints one <key, value> pair per line, separated by a space,
*       using << on each.  You can only call dump if << is defined
*       for your ValueType.  << is already defined for built-in types
*       in C++.  For types you define yourself you may need to add a
*       << operator.
*

To call dump, << needs to be defined for the value type you use. You can define one here that just prints out a little info about the Class so you make sure you have put in the correct entries. OR for the classSymTab, you can just call dumpKeys (another SymTab func) that just prints the keys and not the values.

If you don't know the syntax for overloading <<, there is an example of the header and the func def for one in StringTab.h and StringTab.cc, respectively. That function is for type Symbol, and that's why we don't get the compile error when we do the dump's in testSymTab.cc.


The University of Southern California does not screen or control the content on this website and thus does not guarantee the accuracy, integrity, or quality of such content. All content on this website is provided by and is the sole responsibility of the person from which such content originated, and such content does not necessarily reflect the opinions of the University administration or the Board of Trustees