I had the same problem as this previous question here: Use of undeclared identifier in C++ with templates and inheritance
To sum it up, we try to access a template class' protected attribute from a child class. The described way to do this would be to use this->attribute
instead of only attribute
.
The thing is, I was wondering how come visual studio 2012 didn't need to add the this-> in front of the variable reference for the program to compile and execute properly. I was also wondering if there was a way to have that feature in gcc or other compilers on OS X.
EDIT: Here is the code I used to test this out in visual studio 2012.
//file a.h
template<class T>
class a
{
public:
a(){value = 2;};
protected:
T value;
};
template<class T>
class b: public a<T>
{
public:
T getValue(){return value;};
};
//file main.cpp
#include <iostream>
#include "a.h"
using namespace std;
int main()
{
b<int> myTest;
cout<<myTest.getValue();
system("pause");
return 0;
}
This doesn't compile using g++ but does using visual studio 2012.