Challenge:
I have this code that fails to compile. Can you figure out what’s wrong? It caused headache to me once.
// header
namespace values {
extern std::string address;
extern int port;
}
// .cpp file
std::string ::values::address = "192.0.0.1";
int ::values::port = 12;
It looks correct on the first sight. How many and which are the errors!?
One error:
is the proper form, otherwise the parse is
and there is no member “values” with a member “address” inside “string”…
it will work for builtin types, as they cannot ever contain members.. so int::values is an unambigous parse, int ::values, because the prior doesn’t make sense.
works too. Note that if you typedef int sometype; that you’d have the same problem using sometype as you do with string above, but not with “int”.