I know where the error is in the following code, line 90, I just cannot figure out why it would behaves the way it does:
83 string
84 getStringId(TokenType toktype)
85 {
86 map<TokenType, string>::iterator pos;
87 pos = TokenTypeMap.find(toktype);
88 string str;
89 if(pos != TokenTypeMap.end()) {
90 str = pos->first;
91 }
92 if(str.empty()) {
93 cerr << "Error: Invalid TokenType: " << toktype << endl;
94 exit(EXIT_FAILURE);
95 }
96 /* The following if block is only here for debugging */
97 if(str.length() == 1) {
98 cerr << "Error: String length == 1 :>" << str << "<:"<< endl;
99 exit(EXIT_FAILURE);
100 }
101 return str;
102 }
On line 90 I assign a TokenType to a string. Since TokenType is an enumeration I was expecting something to go bang here due to strict typing but unfortunately there's no warning at all due to it really being an int. The reason for this was found here:
Why does C++ allow an integer to be assigned to a string?
This is fine but, I noticed that the string is not empty and has a length of 1 as would be expected with a char. However, if I try and print it out online 98 I get
Error: String length < 2 :><:
Note there is nothing printed between the markers >