could not Convert | Error returning object

Asked

Viewed 186 times

0

The purpose of the code below is to return the tokens of a particular expression, for example: 50+30*(30-10)+(10/2). Where the tokens would be: [number, 50], [sum, +], [number, 30], [mult,*] and so on.

However when trying to return the type object Token, containing the type (enum) and the value of token requested via getToken(), the following error is displayed:

[Error] could not Convert '(Std::basic_stringstream<_Chart, _Traits, _Alloc>::str() const with _Chart = char; _Traits = Std::char_traits; _Alloc = Std::allocator; Std::basic_stringstream<_Chart, _Traits, _Alloc>::__string_type = Std::basic_string, (Operator new(16ull), (, ((Token*)))' from 'Token*' to 'Token'

main.cpp

#include <iostream>
#include "Scanner.h"
#include "Token.h"
#include <cstdlib>

int main() {

    Scanner* scan = new Scanner("48 + 20 * 2");
    Token tk = scan->getToken();

    while(tk != NULL){
        cout<<tk.print()<<endl;
        tk = scan->getToken();
    }
    return 0;
}

Scanner. h

#ifndef SCANNER_H
#define SCANNER_H

#include <string>
#include <iostream>
#include <ctype.h>
#include <sstream>

#include "Token.h"

using namespace std;

class Scanner
{
    public:
        Scanner(string exp);
        Token getToken();

    private:
        string expressao;
        int posicao;
};

#endif

Scanner.cpp

#include "Scanner.h"

Scanner::Scanner(string exp): expressao(exp), posicao(0){}

Token Scanner::getToken()
{
    std::stringstream valorToken;
    string target;

    while(expressao[posicao] == ' '){
        posicao++;
    }

    if(isdigit(expressao[posicao])){
        valorToken << expressao[posicao];
        posicao++;
        while(posicao < expressao.length() && isdigit(expressao[posicao])){
            valorToken << expressao[posicao];
            posicao++;
        }
        return new Token(numero, valorToken.str());
    }
    else if (this->expressao[posicao] == '+')
    {
        valorToken << "+";
        valorToken >> target;
        return new Token(soma, target);
        posicao++;
    }
    else if (this->expressao[posicao] == '-')
    {
        return new Token(sub, "-");
        posicao++;
    }
    else if (this->expressao[posicao] == '*')
    {
        return new Token(mult, "*");
        posicao++;
    }
    else if (this->expressao[posicao] == '/')
    {
        return new Token(divisao, "/");
        posicao++;
    }
    else
    {
        return new Token(erro, this->expressao[posicao]+"");
        posicao++;
    }
}

Token. h

#ifndef TOKEN_H
#define TOKEN_H

#include <string>
#include <iostream>

using namespace std;

enum TipoToken {
    soma,
    erro,
    numero,
    sub,
    mult,
    divisao
};

class Token
{
    private:
        string valor;
        TipoToken tipo;
    public:
        Token(TipoToken tipo, string valor);
        string print();
};

#endif

Token.cpp

#include "Token.h"

Token::Token(TipoToken tipo, string valor)
{
    this->tipo = tipo;
    this->valor = valor;
}

string Token::print()
{
    if (this->tipo == soma)
    {
        return "Soma: " + this->valor;
    }
    else if (this->tipo == numero)
    {
        return "Numero: " + this->valor;
    }
    else if (this->tipo == sub)
    {
        return "Sub: " + this->valor;
    }
    else if (this->tipo == mult)
    {
        return "Mult: " + this->valor;
    }
    else if (this->tipo == divisao)
    {
        return "Div: " + this->valor;
    }
    else
    {
        return "Token invalido: " + this->valor;
    }
}

1 answer

2


One of the errors is happening in the method Scanner::getToken(). The method signature indicates that an object of the type will be returned Token, however, in the implementation of the method is being returned a new Token();, This is a pointer to Token.

In this situation, there are some solutions:

  • Change the method signature to return one Token* rather than Token.

  • Change the return of the method to return a statically allocated variable (remove the new of return).

The other error is due to comparison in while, that can be accomplished by creating the method getValor();

main.cpp

Scanner *scan = new Scanner("(48 + 30)/2 (15 - 10)");
Token tk = scan->getToken();

while(tk.getValor() != ""){
    cout<<tk.print()<<endl;
    tk = scan->getToken();
}

Token. h

class Token
{
    private:
            string valor;
            TipoToken tipo;
    public:
            Token(TipoToken tipo, string valor);
            string print();
            string getValor();
            TipoToken getTipo();
};

Token.cpp

string Token::getValor(){
    return this->valor;
}

TipoToken Token::getTipo()
{
    return this->tipo;
}
  • I would say return an object Token by value would also be an option, but would need to make a call to the Token, not to the new

  • Hello @Monari or Jeffersonquesado, we can continue this debate via chat?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.