2017-02-19 2 views
-3

Я возвращаюсь к студенту, и мой код действительно ржавый с тех пор, как он был годами. У нас есть программа, где он дал нам шаблон UML. В любом случае, я получаю ошибку c2259, не могу создавать абстрактные классы, когда компилирую, и я не могу найти проблему. Есть идеи?C++ Не удается создать абстрактную ошибку класса

#ifndef OLISTTYPE_H 
#define OLISTTYPE_H 

#include "ListType.h" 

template <class T> 
class OListType: public ListType<T> { 
public: 

bool insert(const T&); 
void insertFirst(const T&); 
void insertLast(const T&); 
bool find(const T&) const; 

}; 


template <class T> 
bool OListType<T>::find(const T& item) const { 
NodeType<T>* temp = this->head; 
while (temp != NULL && temp->info < item) { 
    temp = temp->link; 
} 
return(temp != NULL && temp->info == item); 
} 


template <class T> 
bool OListType<T>::insert(const T& newItem) { 
NodeType<T> *current; 
NodeType<T> *trailCurrent; 
NodeType<T> *newNode; 

bool found; 

newNode = new NodeType<T>; 
newNode->info = newItem; 
newNode->link = nullptr; 

if (first == nullptr) { 
    first = newNode; 
    last = newNode; 
    count++; 
} 
else { 
    current = first; 
    found = false; 
    while (current != nullptr && !found) 
     if (current->info >= newItem) 
      found = true; 
     else { 
      trailCurrent = current; 
      current = current->link; 
     } 

     if (current == first) { 
      newNode->link = first; 
      first = newNode; 
      count++; 
     } 
     else { 
      trailCurrent->link = newNode; 
      newNode->link = current; 
      if (current == nullptr) 
       last = newNode; 
      count++; 
     } 
} 
} 
template<class T> 
void OListType<T>::insertFirst(const T& newItem) { 
insert(newItem); 
} 
template<class T> 
void OListType<T>::insertLast(const T& newItem) { 
insert(newItem); 
} 
#endif 

ListType.h


#ifndef LISTTYPE__H 
#define LISTTYPE__H 

#include <cstddef> 
#include "Nodetype.h" 
#include <iostream> 

template <class T> 

class ListType { 

public: 
ListType(); 
ListType(const ListType<T>&); 
virtual ~ListType(); 
bool operator=(const ListType<T>& right) const; 
virtual bool insert(const T&) = 0; 
virtual bool eraseAll(); 
virtual bool erase(const T&) = 0; 
bool find(const T&) const; 
size_t size() const; 
bool empty() const; 
friend std::ostream& operator<< (std::ostream&, const ListType&); 

protected: 
int count; 
NodeType<T> *first; 
NodeType<T> *last; 
private: 
void destroy(); 
void copy(const ListType<T>&); 


}; 

template <class T> 
ListType<T>::ListType() 
{ 
first = nullptr; 
last = nullptr; 
count = 0; 
} 

template <class T> 
ListType<T>::ListType(const ListType<T>& otherList) { 
first = nullptr; 
copyList(otherList); 
} 

template <class T> 
    ListType<T>::~ListType() { 
destroy(); 
    } 

template<class T> 
bool ListType<T>::operator=(const ListType<T>& right) const 
{ 
return (current = right.current); 
} 



template <class T> 
bool ListType<T>::eraseAll() { 
head = 0; 
return true; 
} 
template <class T> 
bool ListType<T>::erase(const T& it) { 
if (head == 0) 
{ 
    return false; 
} 
else 
{ 
    struct NodeType<T> *u = head->next, *p = head; 
    if (head->item == it) 
    { 
     head = head->next; 
     return true; 
    } 
    while (u != 0 && u->next != 0) 
    { 
     if (u->item == it) 
     { 
      p->next = u->next; 
      return true; 
     } 
     u = u->next; 
     p = p->next; 
    } 
    if (u->next == 0 && u->item == it) 
    { 
     p->next = 0; 
     return true; 
    } 
    return false; 
} 
} 
template <class T> 
bool ListType<T>::find(const T& it) const { 
if (head == 0) 
{ 
    return false; 
} 
else 
{ 
    struct NodeType<T> *p = head; 
    while (p->next != 0) 
    { 
     if (p->item == it) 
     { 
      return true; 
     } 
     p = p->next; 
    } 
    return false; 
    } 
    } 

template <class T> 
size_t ListType<T>::size() const { 
return count; 
} 

template <class T> 
bool ListType<T>::empty() const 
{ 
    return (first == nullptr); 
} 



template <class T> 
void ListType<T>::destroy() { 
NodeType<T> *temp; 
while (first != nullptr) 
{ 
    temp = first; 
    first = first->link; 
    delete temp; 
} 

last = nullptr; 
count = 0; 
} 



template <class T> 
void ListType<T>::copy(const ListType<T>& otherList) { 
nodeType<Type> *newNode; 
nodeType<Type> *current; 

if (first != nullptr) 
    destroylist(); 
if (otherList.first = nullptr) 
{ 
    first = nullptr; 
    last = nullptr; 
    count = 0; 
} 
else { 
    current = otherList.first; 
    count = otherList.count; 
    first = new NodeType<T>; 
    first->info = current->info; 
    first->link = nullptr 
    last = first; 
    current = current->link; 
    while (current != nullptr) { 
     newNode = new NodeType<Type>; 
     newNode->info = current->info; 
     newNode->link = nullptr; 
     last->link = newNode; 
     last = newNode; 
     current = current->link; 
    } 
} 
} 

template <class T> 
std::ostream &operator<< (std::ostream &out, const ListType<T> &list) { 
if (list.head) { 
    NodeType<T> *temp = list.head; 
    out << list.head->item; 
    temp = temp->next; 
    while (temp != 0) { 
     out << "," << temp->item; 
     temp = temp->next; 
    } 
} 
return out; 
} 
#endif 
+0

'OListType' не может переопределить чистую виртуальную функцию' erase() ', так как этот шаблон также заканчивает объявление абстрактного класса, который, как вы знаете, не может быть создан. В чем ваш вопрос? –

+0

Несомненно, пробелы и форматирование присутствовали в ваших оригинальных школьных днях :) – jww

ответ

0

Когда компилятор выдаст сообщение об ошибке, это означает, что вы не выполнили все чисто виртуальные методы базового класса (те, с «= 0» после объявления) в производном классе.

Это означает, что ваш производный класс по-прежнему является абстрактным, поэтому он не может быть создан.

В базовом классе всего два чистых виртуальных метода, вам не нужен метод erase().

+0

Чаще всего говорить «чистый виртуальный» *, чем «абстрактный виртуальный» *. –

+0

thx, исправлено ... – zett42

Смежные вопросы