开发者

C++ include statement required if defining a map in a headerfile

开发者 https://www.devze.com 2022-12-25 11:46 出处:网络
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 symbol

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(t开发者_StackOverflow中文版able.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.
} 


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>.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消