tags:

views:

163

answers:

2

I was doing a project for computer course on programming concepts. This project was to be completed in C++ using Object Oriented designs we learned throughout the course. Anyhow, I have two files symboltable.h and symboltable.cpp. I want to use a map as the data structure so I define it in the private section of the header file. I #include <map> in the cpp file before I #include "symboltable.h".

I get several errors from the compiler (MS VS 2008 Pro) when I go to debug/run the program the first of which is:

Error   1   error C2146: syntax error : missing ';' before identifier 'table'   c:\users\jsmith\documents\visual studio 2008\projects\project2\project2\symboltable.h   22  Project2

To fix this I had to #include <map> in the header file, which to me seems strange.

Here are the relevant code files:

// symboltable.h
#include <map>

class SymbolTable {
    public:
        SymbolTable() {}
        void insert(string variable, double value);
        double lookUp(string variable);
        void init(); // Added as part of the spec given in the conference area.
    private:
        map<string, double> table; // Our container for variables and their values. 
};

and

// symboltable.cpp
#include <map>
#include <string>
#include <iostream>
using namespace std;

#include "symboltable.h"

void SymbolTable::insert(string variable, double value) {
    table[variable] = value; // Creates a new map entry, if variable name already exist it overwrites last value.
}

double SymbolTable::lookUp(string variable) {
    if(table.find(variable) == table.end()) // Search for the variable, find() returns a position, if thats the end then we didnt find it.
        throw exception("Error: Uninitialized variable");
    else
        return table[variable];
}

void SymbolTable::init() { 
    table.clear(); // Clears the map, removes all elements.
} 
+2  A: 

My guess is that you have another file that includes the header file #include "symboltable.h". And that other source file doesn't #include <map> nor #include <string> nor has using namespace std before it includes "symboltable.h".

Check which file is being compiled when you get the error. Is it maybe a different source file than the .cpp that you mentioned? Possibly something like main.cpp?

Another way to solve your problem is to put the includes you need in your header file and use std::map instead of simply map. Also you use string which is also inside the namespace std. So that needs to be std::string. And put the missing #include <string>.

Brian R. Bondy
There probably is a `using namespace std;` somewhere in the code, since the OP says that it works if `<map>` is included.
sth
Ok, I understand these namespace requirement changes. I was under the impression that a header file was just a specification and does not need include statements for external classes. I thought that was handled in the cpp files because the preprocessor just "inserts" the files into place before any other compiler task begin.
Justin
@Justin: Re-read as I made some edits, but yes your understanding is correct. The pre-processing directive `#include` is all done before any C++ compiling is done.
Brian R. Bondy
@Justin: Check which file is being compiled when you get that error. Maybe something like main.cpp?
Brian R. Bondy
Brian: You're correct about it being included somewhere else before map is included. I was so focused on the errors I didn't stop to think which files were linked to where. In my main file, I include the header file so I can instantiate a SymbolTable. Thanks.
Justin
@Justin: No problem, I guess the thing to look out for is when you have errors in a header file always take into consideration which .cpp file is being compiled.
Brian R. Bondy
A: 

Yes, you indeed have to #include <map> in the header file.

You use map in the declaration of the class, so the compiler needs to know what this map refers to. Since the definition of it is in <map> you need to include that header before using the map template class.

You could also instead #include <map> in every source file before the #include "symboltable.h" line, but usually you would just include these kind of prerequisites in the header.

sth