C++ 指定模板化基类作为派生类变量的源

C++ 指定模板化基类作为派生类变量的源,c++,C++,我目前正在将一些代码从VisualStudio移植到mingw,当基类中的变量在派生类中的方法中使用时,它们在代码中是实例。比如说 template<typename t> struct base_foo { int foo; }; template<typename t> struct derived_foo : public base_foo<t> { void some_method(); }; template<typename

我目前正在将一些代码从VisualStudio移植到mingw,当基类中的变量在派生类中的方法中使用时,它们在代码中是实例。比如说

template<typename t>
struct base_foo 
{
  int foo;
};

template<typename t>
struct derived_foo : public base_foo<t> 
{
  void some_method();
};

template<typename t>
void derived_foo<t>::some_method()
{
  foo = 12 ; //Error variable not declared in this scope
  base_foo<t>::foo = 12 ; //OK
}
然后试试看

base_foo<t>::foo = 12;
base_foo::foo=12;

也可以使用
这个->
来代替
base\u foo::
。更好的选择是修复所有损坏的代码。注意,你也可以说
this->foo
。实际上,可能是更好的选择。
base_foo<t>::foo = 12;