This is my first time to try using STL maps. The code looks correct as far as I can tell, but when I try to compile a test file that does nothing but include the header for the class and declare a variable of that class, I am getting compiler errors saying that the map members of my class are not declared in the scope of my function definitions. I am also getting an error that seems to be saying that there is no matching call error for a call to map::insert. Any help with finding my mistakes would be appreciated. Below I have included the compiler errors, my header (.h) file, and my implementation file (.cpp) file I have put comments above the lines in my code that the compiler is complaining about to highlight them. Thanks in advance for any advice.
RefEnv.cpp: In function ‘std::_Rb_tree_iterator<std::pair<const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, VarIdent> > get_var(std::string)’:
RefEnv.cpp:45: error: ‘var’ was not declared in this scope
RefEnv.cpp: In function ‘std::_Rb_tree_iterator<std::pair<const std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, FuncIdent> > get_func(std::string)’:
RefEnv.cpp:58: error: ‘func’ was not declared in this scope
RefEnv.cpp: In member function ‘bool RefEnv::insert_func(Type, std::string, std::string)’:
RefEnv.cpp:104: error: no matching function for call to ‘std::pair<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, VarIdent>::pair(std::string&, FuncIdent&)’
/usr/include/c++/4.4/bits/stl_pair.h:83: note: candidates are: std::pair<_T1, _T2>::pair(const _T1&,
const _T2&) [with _T1 = std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 =
VarIdent]
/usr/include/c++/4.4/bits/stl_pair.h:79: note: std::pair<_T1, _T2>::pair() [with _T1 =
std::basic_string<char, std::char_traits<char>, std::allocator<char> >, _T2 = VarIdent]
/usr/include/c++/4.4/bits/stl_pair.h:68: note: std::pair<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, VarIdent>::pair(const std::pair<std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, VarIdent>&)
#include <map>
#include <string>
#ifndef REFENV_H
#define REFENV_H
enum Type {INT, SEM, PRCD, PRCS};
struct FuncIdent{
std::string stringName; //string name associated with addr
Type type; //type attribute
size_t addr; //array index in func table
};
struct VarIdent{
Type type; //type attribute
bool paramFlag; //true if the variable is a function parameter
size_t addr; //array index in func table
};
class RefEnv{
public:
//constructor
RefEnv();
//set prev pointer
void set_prev(RefEnv*);
//get prev
RefEnv* get_prev();
//return iterator to VarIdent object in var
std::map<std::string, VarIdent>::iterator get_var(std::string);
//return iterator to FuncIdent object in func
std::map<std::string, FuncIdent>::iterator get_func(std::string);
//insert variable attributes in var
bool insert_var(Type, std::string, bool);
//insert function attributes in func
bool insert_func(Type, std::string, std::string);
private:
RefEnv *prev; //pointer to previous reference environment
//Declarations of maps
std::map<std::string, VarIdent> var; //table of sem and int attributes
std::map<std::string, FuncIdent> func; //table of prcd and prcs attributes
};
#endif
#include "RefEnv.h"
#include <map>
#include <string>
RefEnv::RefEnv() : prev(NULL){
}
void RefEnv::set_prev(RefEnv* p){
//Precondition:
// A RefEnv object exists and RefEnv p exists
//
//Postcondition:
// prev in the first RefEnv object points to RefEnv p
prev = p;
}
RefEnv* RefEnv::get_prev(){
//Precondition:
// A RefEnv object exists
//
//Postcondition:
// prev is returned as a constant pointer
return prev;
}
std::map<std::string, VarIdent>::iterator get_var(std::string s){
//Precondition:
// A RefEnv object exists
//
//Postcondition:
// If a VarIdent object is indexed by s,
// an iterator to the object is returned
// Else the iterator to end is returned
//The compiler complains that var is not declared in this scope
return var.find(s);
}
std::map<std::string, FuncIdent>::iterator get_func(std::string s){
//Precondition:
// A RefEnv object exists
//
//Postcondition:
// If a FuncIdent object is indexed by s,
// an iterator to the object is returned
// Else the iterator to end is returned
//The compiler complains that func is not declared in this scope
return func.find(s);
}
bool RefEnv::insert_var(Type t, std::string key, bool p){
//Precondition:
// A RefEnv object exists
//
//Postcondition:
// If there is not a VarIdent object indexed by key in var,
// an object with type t and paramFlag p is inserted
// and true is returned
// Else false is returned
VarIdent vi;
vi.type = t;
vi.paramFlag = p;
vi.addr = var.size();
std::pair<std::map<std::string, VarIdent>::iterator, bool> inserted;
inserted = var.insert(std::pair<std::string, VarIdent>(key, vi));
return inserted.second;
}
bool RefEnv::insert_func(Type t, std::string key, std::string n){
//Precondition:
// A RefEnv object exists
//
//Postcondition:
// If there is not a FuncIdent object indexed by key in func,
// an object with type t and paramFlag p is inserted
// and true is returned
// Else false is returned
FuncIdent vi;
vi.type = t;
vi.stringName = n;
vi.addr = func.size();
std::pair<std::map<std::string, VarIdent>::iterator, bool> inserted;
//The compiler says there is no matching function call for the line below
inserted = func.insert(std::pair<std::string, VarIdent>(key, vi));
return inserted.second;
}