는 Heres는 내 코드 :파생 클래스에서 기본 클래스에서 보호 된 멤버에 액세스 할 수 없습니다
#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;
class root
{
protected :
int size;
double *array;
public :
virtual ~root() {}
virtual root* add(const root&) = 0;
virtual root* sub(const root&) = 0;
virtual istream& in(istream&, root&) = 0;
virtual int getSize() const = 0;
virtual void setSize(int);
};
class aa: public root
{
public :
aa();
aa(int);
aa(const aa&);
root* add(const root& a);
root* sub(const root& a);
istream& in(istream&, root&){}
int getSize() const;
void setSize(int);
};
class bb: public root
{
public:
bb() { }
bb(const bb& b) { }
root* add(const root& a);
root* sub(const root& a);
istream& in(istream&, root&){}
int getSize() const{}
void setSize(int){}
};
aa::aa()
{
size = 0;
array = NULL;
}
aa::aa(int nsize)
{
size = nsize;
array = new double[size+1];
for(int i=0; i<size; i++)
array[i] = 0;
}
root* aa::add(const root& a)
{
for (int i=0; i<a.size; i++)
array[i] += a.array[i];
return *this;
}
root* aa::sub(const root& a)
{
}
int aa::getSize() const
{
return size;
}
void aa::setSize(int nsize)
{
size = nsize;
array = new double[size+1];
for(int i=0; i<size; i++)
array[i] = 0;
}
root* bb::add(const root& a)
{
return new bb();
}
root* bb::sub(const root& a)
{
}
int main(int argc, char **argv)
{
}
내가 size
에 액세스하려는 또는 파생 클래스의 array
, 난 그냥 내 컴파일러는 캔트 말한다 때문에 :
/home/brian/Desktop/Temp/Untitled2.cpp||In member function ‘virtual root* aa::add(const root&)’:|
/home/brian/Desktop/Temp/Untitled2.cpp|10|error: ‘int root::size’ is protected|
/home/brian/Desktop/Temp/Untitled2.cpp|66|error: within this context|
/home/brian/Desktop/Temp/Untitled2.cpp|11|error: ‘double* root::array’ is protected|
/home/brian/Desktop/Temp/Untitled2.cpp|67|error: within this context|
/home/brian/Desktop/Temp/Untitled2.cpp|68|error: cannot convert ‘aa’ to ‘root*’ in return|
||=== Build finished: 5 errors, 0 warnings ===|
보호 된 멤버가 파생 클래스에서 비공개 인 것을 읽었으므로 괜찮은 것으로 보이지만 그렇지 않습니다. 이 문제를 해결하는 방법?
감사합니다. 그럼,이 문제를 해결할 방법이 있습니까? –
@BrianBrown :'root'에'size()'라고 불리는'public' getter 함수를 사용할 수 있습니까? –
''root''에서 이미 정의했지만 순수 가상 (그것은 잘못된 것입니까?). 그러나 배열은 어때? –