내 대학의 기본 C++ 강의에이 작업을 수행해야하므로 분명히 알 수 있습니다. 허용 된 경우 STL을 사용했을 것입니다.추상 클래스를 사용하여 파생 클래스의 요소 스택 구현
문제점 : "cube"와 "sphere"클래스에서 파생 된 "shape3d"라는 클래스가 있습니다. 이제 "shape3d_stack"을 구현해야하는데, 이는 "큐브"및 "구"유형의 객체를 보유 할 수 있어야한다는 의미입니다. 이 배열을 사용하고 int 스택을 사용하여 이렇게 할 때 꽤 잘 작동했습니다. 그래서처럼하려고 노력 :
shape3d_stack.cpp :
15 // more stuff
16
17 shape3d_stack::shape3d_stack (unsigned size) :
18 array_ (NULL),
19 count_ (0),
20 size_ (size)
21 { array_ = new shape3d[size]; }
22
23 // more stuff
하지만, 불행하게도, 컴파일러는 나에게 말한다 :
g++ -Wall -O2 -pedantic -I../../UnitTest++/src/ -c shape3d_stack.cpp -o shape3d_stack.o
shape3d_stack.cpp: In constructor ‘shape3d_stack::shape3d_stack(unsigned int)’:
shape3d_stack.cpp:21: error: cannot allocate an object of abstract type ‘shape3d’
shape3d.hpp:10: note: because the following virtual functions are pure within ‘shape3d’:
shape3d.hpp:16: note: virtual double shape3d::area() const
shape3d.hpp:17: note: virtual double shape3d::volume() const
난이 정말 못생긴 디자인의 일종해야합니다 생각 혼자서 발생한 오류. 그렇다면 "shape3d"에서 파생 된 모든 종류의 객체를 스택에 사용하는 올바른 방법은 무엇입니까?