I have some header files which host functions and in other enums.
So in file Lexer.h, I have a function called getNextToken()
which returns a token and in this function I need to call a function called reservedLookup(string tokenString)
found in token.h
reservedWords is another header file called reservedWords.h
which has enum declarations of the reserved Words
This function is found in token.h
reservedWords reservedLookup (string tokenString)
{
for(rIt = reservedMap.begin(); rIt!= reservedMap.end(); rIt++)
{
if(tokenString == (rIt->first))
{
return rIt->second;
}
}
}
in lexer.h I tried using this in private (and even in public:) :
reservedWords reservedLookup(string tokenString);
it compiles, but when in function Token* getNextToken()
I use
int tokenType = reservedLookup(strBuffer);
it gives me an error stating:
obj\Release\main.o:main.cpp:(.text$_ZN5Lexer12getNextTokenEv[__ZN5Lexer12getNextTokenEv]+0x371)||undefined reference to `Lexer::reservedLookup(std::string)'|
I don't want my compiler to read reservedLookup as part of Lexer::reservedLookup(string str)
but as Token::reservedLookup(string str)
Is there any way I can do it?
EDIT:
Token.h
class Token
{
.....
.....
public:
void reservedDeclare ()
{
// iterator used for looping through reservedWords
//Taking care of the Reserved Words first
reservedMap["function"] = reservedWords::tkfunction;
//if - then - else - while - halt
reservedMap["if"] = reservedWords::tkif;
reservedMap["else"] = reservedWords::tkelse;
reservedMap["while"] = reservedWords::tkwhile;
reservedMap["halt"] = reservedWords::tkhalt;
//and, or, not, true, else
reservedMap["and"] = reservedWords::tkand;
reservedMap["or"] = reservedWords::tkor;
reservedMap["not"] = reservedWords::tknot;
reservedMap["true"] = reservedWords::tktrue;
reservedMap["false"] = reservedWords::tkfalse;
//sets and read/write
reservedMap["set"] = reservedWords::tkset;
reservedMap["let"] = reservedWords::tklet;
reservedMap["read"] = reservedWords::tkread;
reservedMap["write"] = reservedWords::tkwrite;
//variable type
reservedMap["int"] = reservedWords::tkint;
reservedMap["char"] = reservedWords::tkchar;
reservedMap["bool"] = reservedWords::tkbool;
reservedMap["real"] = reservedWords::tkreal;
reservedMap["string"] = reservedWords::tkstring;
reservedMap["unit"] = reservedWords::tkunit;
}
reservedWords reservedLookup (string tokenString)
{
for(rIt = reservedMap.begin(); rIt!= reservedMap.end(); rIt++)
{
if(tokenString == (rIt->first))
{
return rIt->second;
}
}
}
reservedWords.h
#ifndef RESERVEDWORDS_H_INCLUDED
#define RESERVEDWORDS_H_INCLUDED
#include <string>
#include <vector> //std::vector
#include <algorithm> // std::find
using namespace std;
/**
All the reserved words used by the compiler
*/
/**
This file contains all the keywords or reserved words or reserved Symbols used by the compiler
In the lexer, we will check whether the string we are passing is either a Reserved word
or it is actually and identifier
*/
enum reservedWords
{
tkfunction,
tkif,
tkelse,
tkwhile,
tkhalt,
tkand,
tkor,
tknot,
tktrue,
tkfalse,
tkset,
tklet,
tkread,
tkwrite,
tkint,
tkchar,
tkbool,
tkreal,
tkstring,
tkunit,
tkreservedWord,
tkidentifier
};
#endif // RESERVEDWORDS_H_INCLUDED
PARTIAL Code of Lexer.h
class Lexer
{
private:
string strBuffer ="";//holds the current characters that have been read and waiting to be matched
int tokenType = 0;
reservedWords reservedLookup(string tokenString); // THIS DOES NOT WORK. SEES IT AS Lexer::reservedLookup
....
....
...
...
tokenType = reservedLookup(strBuffer); // GIVES ME ERROR BECAUSE OF ABOVE