c ++错误初始化父类的属性

时间:2014-01-17 20:19:49

标签: c++

我遇到了问题,因为派生类无法初始化父类的属性。在下面的代码中,错误在主要部分给出。 (我知道这段代码很愚蠢,但我已经简化了)

class.cpp

# include < iostream >
# include < stdexcept >
using namespace std;

template < typename T > class A;
template < typename T > class B;

template < typename T > class A{

protected:

T **m;
int r;

void allocate_mem(T ***ptr){
    *ptr = new T*[1];
    (*ptr)[0] = new T[1];
}

public:

A(){
    throw logic_error("Error!");
}

~A(){
    delete[] m[0];
    delete[] m;
}

};

template < typename T > class B: public A<T>{

public:

B(int val):
A<T>::r(0){                 //no type named 'r' in 'class A<int>'
    A<T>::allocate_mem(& this->m);
    A<T>::m[0][0] = val;
 }

};

的main.cpp

int main(int argc, char **argv){

B <int> a(4);  //here the error is called

return 0;

}

0 个答案:

没有答案