从纯虚拟模板派生 class
Deriving from pure virtual template class
我认为我的问题很简单,但我无法克服它。
我有抽象模板 class aghContainer 和子模板 class aghVector。我正在尝试制作 aghVector 对象,但出现无法创建抽象对象 class 的错误。我很确定我实现了所有方法,但也许我错过了一些东西...
agh容器:
template <typename T>
class aghContainer {
public:
virtual ~aghContainer() {}
virtual void append(T const&) = 0;
virtual void append(aghContainer<T> const& right) = 0;
virtual bool replace(const int, T const&) = 0;
virtual T& at(const int) const = 0;
virtual int size(void) const = 0;
virtual bool remove(const int) = 0;
virtual void clear(void) = 0;
virtual bool isEmpty(void) = 0;
virtual int indexOf(T const& _value, int _from = 0) const = 0;
virtual bool contains(T const& _value, int _from = 0) const = 0;
virtual void print(ostream&) const = 0;
virtual bool equal(aghContainer<T> const& right) const = 0;
virtual bool operator==(aghContainer<T> const& right) const;
virtual bool operator!=(aghContainer<T> const& right) const;
virtual T& operator[](const int n) const;
virtual aghContainer<T>& operator+=(T const& element);
virtual aghContainer<T>& operator+=(aghContainer<T> const& right);
virtual aghContainer<T>& operator<<(T const& element);
virtual aghContainer<T>& operator<<(aghContainer<T> const& right);
friend ostream& operator<<(ostream&, aghContainer<T> const& right);
};
aghVector:
template <typename T>
class aghVector :
public aghContainer<T> {
public:
aghVector();
~aghVector();
void append(T const&);
void append(aghContainer<T> const& right);
bool insert(const int, T const&);
bool replace(const int, T const&);
T& at(const int) const;
int size(void) const;
bool remove(const int);
void clear(void);
bool isEmpty(void);
int indexOf(T const& _value, int _from = 0);
bool contains(T const& _value, int _from = 0) const;
void print(ostream&) const;
bool equal(aghContainer<T> const& right) const;
private:
T* vector;
unsigned int elements;
void destroyVector();
};
错误:
'aghVector': cannot instantiate abstract class Data-Container G:\CPP\Data-Container\ex3main.cpp 101
从 aghContainer
class 你有函数
void append(aghContainer<T> const& right) = 0;
然后在 aghVector
你有这个功能:
void append(aghVector<T> const& right);
问题是那是两个不同的函数,这意味着您不会覆盖父 class append
函数。导致 aghVector
仍然是抽象的 class。
我可以快速查看几个问题。
在aghContainer
void append(aghContainer<T> const& right) = 0;
并在 aghVector
void append(aghVector<T> const& right);
实际上用不同的参数类型重载函数(因为 aghVector
是不同于 aghContainer
的类型。继承的 append()
将被隐藏而不是被覆盖。
equal()
的类似问题。
aghContainer
中indexOf()
的声明是const
,aghVector
中的不是。因此,aghVecvtor
中的版本再次隐藏了继承的功能,并没有覆盖它。
我认为我的问题很简单,但我无法克服它。
我有抽象模板 class aghContainer 和子模板 class aghVector。我正在尝试制作 aghVector 对象,但出现无法创建抽象对象 class 的错误。我很确定我实现了所有方法,但也许我错过了一些东西...
agh容器:
template <typename T>
class aghContainer {
public:
virtual ~aghContainer() {}
virtual void append(T const&) = 0;
virtual void append(aghContainer<T> const& right) = 0;
virtual bool replace(const int, T const&) = 0;
virtual T& at(const int) const = 0;
virtual int size(void) const = 0;
virtual bool remove(const int) = 0;
virtual void clear(void) = 0;
virtual bool isEmpty(void) = 0;
virtual int indexOf(T const& _value, int _from = 0) const = 0;
virtual bool contains(T const& _value, int _from = 0) const = 0;
virtual void print(ostream&) const = 0;
virtual bool equal(aghContainer<T> const& right) const = 0;
virtual bool operator==(aghContainer<T> const& right) const;
virtual bool operator!=(aghContainer<T> const& right) const;
virtual T& operator[](const int n) const;
virtual aghContainer<T>& operator+=(T const& element);
virtual aghContainer<T>& operator+=(aghContainer<T> const& right);
virtual aghContainer<T>& operator<<(T const& element);
virtual aghContainer<T>& operator<<(aghContainer<T> const& right);
friend ostream& operator<<(ostream&, aghContainer<T> const& right);
};
aghVector:
template <typename T>
class aghVector :
public aghContainer<T> {
public:
aghVector();
~aghVector();
void append(T const&);
void append(aghContainer<T> const& right);
bool insert(const int, T const&);
bool replace(const int, T const&);
T& at(const int) const;
int size(void) const;
bool remove(const int);
void clear(void);
bool isEmpty(void);
int indexOf(T const& _value, int _from = 0);
bool contains(T const& _value, int _from = 0) const;
void print(ostream&) const;
bool equal(aghContainer<T> const& right) const;
private:
T* vector;
unsigned int elements;
void destroyVector();
};
错误:
'aghVector': cannot instantiate abstract class Data-Container G:\CPP\Data-Container\ex3main.cpp 101
从 aghContainer
class 你有函数
void append(aghContainer<T> const& right) = 0;
然后在 aghVector
你有这个功能:
void append(aghVector<T> const& right);
问题是那是两个不同的函数,这意味着您不会覆盖父 class append
函数。导致 aghVector
仍然是抽象的 class。
我可以快速查看几个问题。
在aghContainer
void append(aghContainer<T> const& right) = 0;
并在 aghVector
void append(aghVector<T> const& right);
实际上用不同的参数类型重载函数(因为 aghVector
是不同于 aghContainer
的类型。继承的 append()
将被隐藏而不是被覆盖。
equal()
的类似问题。
aghContainer
中indexOf()
的声明是const
,aghVector
中的不是。因此,aghVecvtor
中的版本再次隐藏了继承的功能,并没有覆盖它。