Question

char Substitution::encodeChar(char a, std::map<char, char>&){
    return &[a];
}

This is my implementation attempt (based on a pre defined Class header which I may not change for the assignment). In Visual Studio I get the error (see title) over the semicolon?

Trying

&.find(a)

instead gives me "expected an expression" over the period.

I think I spotted somewhere saying something about const char vs. char for this problem, but I can't wrap my head around it. I've used map char char earlier this way, but somehow using it in this context doesn't work.

Was it helpful?

Solution

It looks like you're trying to treat & as a variable name. It's not. Variable names consist only of letters, digits, and underscores.

Actually, in the function parameters, std::map<char, char>& means that the type of the parameter is a "reference to std::map<char, char>". Note that I said "reference to". That's what the & means. It's part of the type and makes the parameter a reference parameter.

So you need to give your parameter a name and then use that name:

char Substitution::encodeChar(char a, std::map<char, char>& my_map){
    return my_map[a];
}

We can read the parameter std::map<char, char>& my_map as saying the my_map is a "reference to std::map<char, char>". Then, my_map[a] accessing the key a in that map.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top