warrior_monk
warrior_monk

Reputation: 433

Expected unqualified id before “.” token

I see the above question(and similar questions) are answered at various places here, here and here, but none of them explains what I am really after.

I also saw a very good explanation of qualified / Un-qualified -id here. Here is my code, I am experimenting with static variables defined in the struct.

#include<iostream>
#include<stdio.h>
#include<string>

using namespace std;
struct  foo {
  static  string name ;
};
string foo::name{"Warrior_Monk"};


int main(){
cout<<"foo::name is "<<foo::name<<endl;

//Chaging the name , and then calling the second name through  a **temp** object which will be created 
foo.name ="Great_Warriror_Monk"; // error:expected unqaulified-id before '.' token

//foo::name ="Great_Warriror_Monk";  // THIS IS  THE FIX
foo temp ; 
cout<<"foo::name is "<<temp.name<<endl;
cout<<"foo::name is "<<foo::name<<endl;
    return 0 ; 
}

Now inside the main(), I unsuccessfully tried to change the static variable name with the command:

foo.name ="Great_Warriror_Monk";

for which GCC gave me the error:

error:expected unqaulified-id before '.' token

I know the fix is:

foo::name ="Great_Warriror_Monk"

But I am more inclined towards understanding the error. The error says that it expected an unqualified-id before the '.' token. From my understanding of unqualified id from here, struct foo well qualifies as an unqualified-id. So does it mean the error is incorrect with its wordings?

Upvotes: 0

Views: 10681

Answers (1)

Nicol Bolas
Nicol Bolas

Reputation: 474116

Yes, foo is an unqualified identifier. However, it's important to remember that when it comes to C++ errors, the text of the error may not be the actual problem. In some cases, you just have to look at the line of code to see what the real problem is. This is doubly so for GCC, which is pretty well known for producing obtuse errors on seemingly simple syntactic issues.

Using a simplified form of your example, both Clang and Visual Studio do a better job of diagnosing this particular problem. VS may only give you a generic "syntax error," but at least it specifically mentions the ., which is more helpful than GCC bringing up irrelevant questions about the qualifications of the identifier. Clang (which has as one of its design goals to create better C++ error messages) just tells you what the problem is.

If you're going to use C++, you're going to have to get used to sometimes obtuse error messages.

Upvotes: 4

Related Questions