0

I am writing a lexical analyzer in C++ and have to include a to_string() method for my subclasses. This method won't be used by the lexical analyzer but we are told we have to include them for debugging purposes. The to_string methods I have written keep generating errors and I am not sure why. Here is what I have:

string *AddopToken::to_string()
{
   token_type_type tokenType = get_token_type();
   addop_attr_type addopAttr = get_attribute();
   return "[TOKEN TYPE: " + tokenType + ", ATTRIBUTE TYPE: " + addopAttr + "]";
}

This seems like it should work, but for some reason it does not.

Here is the typedef for addop_attr_type in the AddopToken header.

typedef enum addop_attr { ADDOP_ADD = 400,
                          ADDOP_SUB = 401,
                          ADDOP_OR = 402,
                          ADDOP_NO_ATTR = 499 } addop_attr_type;

So even though the type of addopAttr is addop_attr_type, all that really is is an int constant. I figured C++ could convert an int to a string. Is there a way to convert these variables to a string so my to_string() will work correctly?

2 Answers2

0

C++ doesn't allow + for concatenation of string literals or string literals with integers. The code you need is

char buff[1204];
sprintf(buff, "[TOKEN_TYPE; %d , ATTRUBUTE_TYPE %d ]", tokenType, addopAttr);

return std::string(buff);

You don't have to use the old C function sprintf, there are also lots of C++ functions people have invented for achieving similar things, and that gets rid of the temporary buffer, but at cost of being harder to follow what is going on underneath.

Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18
  • I tried this and the compiler wouldn't allow it. The error I got said `error: 'sprintf' was not declared in this scope` and the other was `error: cannot convert 'std::string {aka std::basic_string}' to 'std::string {aka std::basic_string in return string(buff)` – user3869795 Oct 03 '16 at 02:53
  • #include to get sprintf. The other error is because you've asked for a pointer to a string instead of a string. In C++ you don't need to do that, it just makes the code harder to write. std;;strings are like variables, not like C strings. – Malcolm McLean Oct 03 '16 at 02:58
  • We were given the code for the header files and in the header, as well as the super class, the `to_string()` methods return type was a pointer `string *`. We were told not to change that. – user3869795 Oct 03 '16 at 03:21
0

This should work for you:

Edit: now it returns pointer to a string, Don't forget to delete the pointer after you finish using it.

 std::string* AddopToken::to_string() const{

           token_type_type tokenType = get_token_type();
           addop_attr_type addopAttr = get_attribute();

           std::string* result = std::string();
           *result = std::string("[TOKEN TYPE: ") + std::to_string(tokenType) + std::string(", ATTRIBUTE TYPE: ") + std::to_string(addopAttr) + std::string("]");

           return result;
        }

C++ see "[TOKEN TYPE: " as char[14] and not as a string. And to convert int to string use std::to_string().

user3196144
  • 197
  • 2
  • 11
  • The method is defined in the super class as `string *` so we must deal with a pointer. How would that affect this? – user3869795 Oct 03 '16 at 03:20
  • Create a new pointer, point it to what you return now and return the pointer. But you will have to remember to delete the pointer when you are done with it. – user3196144 Oct 03 '16 at 03:36
  • I tried this and it didn't work. I don't know why, but for some reason it keeps saying `error: no matching function for call to 'AddopToken::get_attribute() const'` I don't know why it says that because the `get_attribute()` method is defined and working correctly and it's not supposed to be a const method. Does the fact that the signature of `to_string` is `string *AddopToken::to_string() const {....}` – user3869795 Oct 03 '16 at 04:58
  • Yes, const functions must call const functions from the same class. you can use const_cast, but if you are allowed to change get_attribute() to const you do that. – user3196144 Oct 03 '16 at 05:20
  • When you define AddopToken::to_string(), it has to have the same signature as well, string *AddopToken::to_string() const { // Your code }. – user3196144 Oct 03 '16 at 12:56
  • It does have the same signature, In the header file it is listed as `string *` and in the class file I also have it as `string *` – user3869795 Oct 03 '16 at 14:48
  • If to to_string() initialized in the header file with const then when you define to_string() you have to define it with const. – user3196144 Oct 03 '16 at 15:51
  • I edited my code so it much to_string()`s initialization. – user3196144 Oct 03 '16 at 15:53