Why error about "error: nested name specifier 'h::TYPE::' for declaration does not refer into a class, class template or class template partial specialization" show is only mark place?
#include <iostream>
namespace h
{
enum TYPE
{
A, B, C
};
struct test
{
test(TYPE type = B) { std::cout << type << std::endl; }
};
};
int main()
{
h::test t{h::TYPE::A};
h::test r(h::TYPE::A);
h::test {h::TYPE::A};
h::test (h::TYPE::A); <--- only here
}
As I found out from my colleagues. C++ is a pretty funny language. Mainly due to its long history. The meaning of the compilation error is that the problematic line of code: h::test (h::TYPE::A);
The compiler parsed as "creating a variable of type h::test with the name h::TYPE::A". And then the compiler goes crazy, because you can't call variables that way.
All this is because someone once decided that it might be convenient to declare variables in this
int (value) style;
value = 42;
std::string (str);
str = "hello";
In other cases, the compiler parses the code correctly - it understands that this is a call to the constructor of the h::text class.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments