1

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 >

2
  • 3
    Many ASCII character values won't show up as normal characters when you try to print them. Why don't you try printing pos->first as an integer, and see what character you're expecting? Commented Nov 25, 2012 at 1:36
  • Dammit, I should have got that. Commented Nov 25, 2012 at 1:54

1 Answer 1

1

You didn't tell us what the actual value was, but it's probably an unprintable character in ASCII.

This is a rather strange use of strings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.