我收到編譯器錯誤“使用未宣告的識別符號'_storage'”
#include <iostream>
struct Storage{
int t;
};
struct DerivedStorage : Storage{
int s;
};
struct DerivedStorage2 : DerivedStorage{
int r;
};
template<typename DS = Storage>
class Base{
public:
DS* _storage = nullptr;
Base(){
_storage = new DS();
}
void m(){
_storage->t ;
}
};
template<typename DS = DerivedStorage>
class Derived : public Base<DS>{
public:
void m2(){
_storage->s ; //error here
}
};
template<typename DS = DerivedStorage2>
class Derived2 : public Derived<DS>{
public:
void m3(){
_storage->r ; //error here
}
};
int main(int argc, const char * argv[]) {
Derived2<DerivedStorage2> t;
for(int i = 0;i<3;i ){
t.m3();
}
return 0;
}
知道問題是什么嗎?
uj5u.com熱心網友回復:
因為Derived它本身沒有一個名為 的成員變數_storage,所以this->_storage用來訪問Base's_storage代替。
void m2(){
this->_storage->s ;
}
演示
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/438735.html
