Home > Article > Backend Development > Why Can\'t I Access Base Class Members in a Template Function with GCC?
The provided code fails to compile with GCC but succeeds with Visual Studio. When attempting to access the foo member of the base class in the bar function, GCC encounters an error claiming that foo is not declared within the current scope.
According to the official C specifications, GCC adheres to certain rules that prevent the compiler from inferring the base class's members if the base class is a template class. This is because, without direct knowledge of the base class's definition, the compiler cannot determine its members.
To resolve this issue, there are two options:
<code class="cpp">void bar() { cout << this->foo << endl; }
<code class="cpp">void bar() { cout << A<T>::foo << endl; }
This enables GCC to recognize the foo member as belonging to the base class A Hence, the correct syntax for accessing base class members in a template class is this->foo or A The above is the detailed content of Why Can\'t I Access Base Class Members in a Template Function with GCC?. For more information, please follow other related articles on the PHP Chinese website!