I overloaded the operator []
using a template, but when I try to use it, I get the error type name is not allowed
. I have a class Variables
like this:
class Variables {
private:
map<string, Token> variables;
map<string, Funcion> funciones;
public:
void agregar(string nombre, Token token) { variables[nombre] = token; }
template <class T>
T& operator [](const string nombre) {
if(variables.find(nombre) != variables.end()) return variables[nombre];
return funciones[nombre];
}
}
In case you want the Token class is something like this:
class Token {
private:
string valor;
public:
Token();
Token(string valor_): valor(valor_) {}
string getValor(){ return valor; }
};
When obtaining the value using the bracket notation, the editor gives me an error
Token token;
Variables vars;
vars.agregar("holas", Token("holas"));
token = vars<Token>["holas"]; //aqui me marca el error
The indexing operator (
operator []
) cannot be used with a template argument list:So you have to remove it, but then another error comes up:
So you must call the indexing operator in function format:
But then the error is another:
So you should change the statement
return
: