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?