C++ 模板化派生类时访问基成员数据错误

C++ 模板化派生类时访问基成员数据错误,c++,templates,template-meta-programming,C++,Templates,Template Meta Programming,我在尝试访问CRTP基类的数据成员时遇到了一个奇怪的重复出现的模板问题 template<typename T> struct Base { int protectedData=10; }; struct Derived : public Base<Derived> { public: void method() { std::cout<<protectedData<<std::endl; }; }; int main ()

我在尝试访问CRTP基类的数据成员时遇到了一个奇怪的重复出现的模板问题

template<typename T>
struct Base {
  int protectedData=10;
};

struct Derived : public Base<Derived> {
public:
  void method() {
    std::cout<<protectedData<<std::endl;
  };
};

int main ()
{
  Derived a;
  a.method();
}
模板
结构基{
int-protectedData=10;
};
结构派生:公共基{
公众:
void方法(){

std::cout这实际上与CRTP无关,而是与以下事实有关:对于依赖基访问派生代码,您需要对其进行限定

将线路更改为

std::cout<<this->protectedData<<std::endl;

std::如果使用基类名称引用成员,它能工作吗?
base::protectedMember
尝试提供变量的完整作用域。
this->protectedData
protectedData
的名称查找不会查找依赖的基类。谢谢,它能工作。@PiotrS。当然,你是对的。谢谢。Corre这句话很简单。
g++-4.9 test.cc -Wall -std=c++1y -Wconversion -Wextra
test.cc: In member function ‘void Derived<T>::method()’:
test.cc:26:11: error: ‘protectedData’ was not declared in this scope
    cout<<protectedData<<endl;
std::cout<<this->protectedData<<std::endl;