C++ 中的额外限定错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5642367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
extra qualification error in C++
提问by prosseek
I have a member function that is defined as follows:
我有一个定义如下的成员函数:
Value JSONDeserializer::ParseValue(TDR type, const json_string& valueString);
When I compile the source, I get:
当我编译源代码时,我得到:
error: extra qualification 'JSONDeserializer::' on member 'ParseValue'
错误:成员“ParseValue”上的额外限定“JSONDeserializer::”
What is this? How do I remove this error?
这是什么?如何消除此错误?
回答by Sylvain Defresne
This is because you have the following code:
这是因为您有以下代码:
class JSONDeserializer
{
Value JSONDeserializer::ParseValue(TDR type, const json_string& valueString);
};
This is not valid C++ but Visual Studio seems to accept it. You need to change it to the following code to be able to compile it with a standard compliant compiler (gcc is more compliant to the standard on this point).
这不是有效的 C++,但 Visual Studio 似乎接受它。您需要将其更改为以下代码才能使用符合标准的编译器进行编译(在这一点上 gcc 更符合标准)。
class JSONDeserializer
{
Value ParseValue(TDR type, const json_string& valueString);
};
The error come from the fact that JSONDeserializer::ParseValue
is a qualified name (a name with a namespace qualification), and such a name is forbidden as a method name in a class.
错误来自于它JSONDeserializer::ParseValue
是一个限定名称(具有命名空间限定的名称),并且这样的名称在类中被禁止作为方法名称。
回答by joe_coolish
This means a class is redundantly mentioned with a class function. Try removing JSONDeserializer::
这意味着一个类被一个类函数多余地提及。尝试删除JSONDeserializer::
回答by Boaz Yaniv
Are you putting this line insidethe class declaration? In that case you should remove the JSONDeserializer::
.
你把这一行放在类声明中吗?在这种情况下,您应该删除JSONDeserializer::
.
回答by bunkerdive
A worthy note for readability/maintainability:
可读性/可维护性的一个值得注意的地方:
You can keep the JSONDeserializer::
qualifier with the definition in your implementation file (*.cpp).
您可以将JSONDeserializer::
限定符与实现文件 (*.cpp) 中的定义一起保留。
As long as your in-class declaration (as mentioned by others) does not have the qualifier, g++/gcc will play nice.
只要您的类内声明(如其他人提到的)没有限定符,g++/gcc 就会很好地发挥作用。
For example:
例如:
In myFile.h:
在 myFile.h 中:
class JSONDeserializer
{
Value ParseValue(TDR type, const json_string& valueString);
};
And in myFile.cpp:
在 myFile.cpp 中:
Value JSONDeserializer::ParseValue(TDR type, const json_string& valueString)
{
do_something(type, valueString);
}
When myFile.cpp implements methods from many classes, it helps to know who belongs to who, just by looking at the definition.
当 myFile.cpp 实现来自许多类的方法时,只需查看定义就可以知道谁属于谁。