extra qualification error in C++

后端 未结 5 1334
轮回少年
轮回少年 2020-11-30 01:37

I have a member function that is defined as follows:

Value JSONDeserializer::ParseValue(TDR type, const json_string& valueString);

When

相关标签:
5条回答
  • 2020-11-30 01:52

    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).

    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.

    0 讨论(0)
  • 2020-11-30 01:54

    Are you putting this line inside the class declaration? In that case you should remove the JSONDeserializer::.

    0 讨论(0)
  • 2020-11-30 01:57

    I saw this error when my header file was missing closing brackets.

    Causing this error:

    // Obj.h
    class Obj {
    public:
        Obj();
    

    Fixing this error:

    // Obj.h
    class Obj {
    public:
        Obj();
    };
    
    0 讨论(0)
  • 2020-11-30 02:09

    This means a class is redundantly mentioned with a class function. Try removing JSONDeserializer::

    0 讨论(0)
  • 2020-11-30 02:13

    A worthy note for readability/maintainability:

    You can keep the JSONDeserializer:: qualifier with the definition in your implementation file (*.cpp).

    As long as your in-class declaration (as mentioned by others) does not have the qualifier, g++/gcc will play nice.

    For example:

    In myFile.h:

    class JSONDeserializer
    {
        Value ParseValue(TDR type, const json_string& valueString);
    };
    

    And in 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.

    0 讨论(0)
提交回复
热议问题