我对抽象类有点问题。我有一堆课。在StackAsLinkedList.h类中,我有一个指向对象的链接列表,如此处所示
LinkedList<Object*> list;
语法错误,我不知道该怎么做。如果我将它命名为int或char我得到相同的语法错误..我对ADT /类层次结构相当新 我收到了很多错误,因为StackAsLinkedLis.cpp文件需要识别该列表。
这里只有两个文件。我有接近14个.h文件,所以我宁愿不发布所有代码。
#pragma once
#include "stack.h"
#include "List_Element.h"
#include "StackAsLinkedList.h"
#ifndef STACKASLINKEDLIST_H
#define STACKASLINKEDLIST_H
class StackAsLinkedList : public Stack
{
LinkedList<Object*> list;
void StackAsLinkedList::Purge ();
void StackAsLinkedList::Accept (Visitor& visitor) const;
void StackAsLinkedList::Push (Object& object);
virtual Object& StackAsLinkedList::Pop ();
virtual Object& StackAsLinkedList::Top () const;
virtual StackAsLinkedList::~StackAsLinkedList ();
class Iter;
public:
StackAsLinkedList ();
// ...
friend class Iter;
};
class StackAsLinkedList::Iter : public Iterator
{
StackAsLinkedList const& stack;
virtual ListElement<Object*> const* position;
bool StackAsLinkedList::Iter::IsDone () const;
virtual Object& StackAsLinkedList::Iter::operator * () const;
virtual void StackAsLinkedList::Iter::operator ++ ();
void StackAsLinkedList::Iter::Reset ();
public:
Iter (StackAsLinkedList const&);
// ...
};
#endif
这是另一个头文件,我想你可能需要看
#ifndef Linked_List_H
#define Linked_List_H
#include "List_Element.h"
template <class T>
class LinkedList
{
ListElement<T>* head; // Protected member varable //
ListElement<T>* tail; // Protected member varable //
public:
LinkedList ();
~LinkedList ();
LinkedList (LinkedList const&);
LinkedList& operator = (LinkedList const&);
ListElement<T> const* Head () const;
ListElement<T> const* Tail () const;
bool IsEmpty () const; // Function to determine if list is empty //
T const& First () const;
T const& Last () const;
void Prepend (T const&); // Function to insert a node in front of the first node of the list //
void Append (T const&); // Function to insert a node in end of the last node of the list //
void Extract (T const&); // Function to extract a node //
void Purge (); // Function to purge all the data from list before deletion //
void InsertAfter (ListElement<T> const*, T const&); // Function to insert node after a specific node in the list //
void InsertBefore (ListElement<T> const*, T const&); // Function to insert node before a specific node in the list //
};
#endif
这里是stack.h,你需要看到它并且遗漏了什么......
#ifndef STACK_H
#define STACK_H
#include "Container.h"
class Stack : public virtual Container
{
public:
virtual Object& Top () const = 0;
virtual void Push (Object&) = 0;
virtual Object& Pop () = 0;
};
#endif
这是stackaslinklist.cpp文件,它不识别列表
#include "StackAsLinkedList.h"
#include "NullObject.h"
#include "List_Element.h"
#include "Stack.h"
#include "Linked_List.h"
#include "Container.h"
//void StackAsLinkedList::Accept (Visitor& visitor) const
//{
// ListElement<Object*> const* ptr;
//for (ptr = list.Head ();
//ptr != 0 && !visitor.IsDone (); ptr = ptr->Next ())
// {
//visitor.Visit (*ptr->Datum ());
// }
//}
StackAsLinkedList::StackAsLinkedList () : list ()
{
}
void StackAsLinkedList::Purge ()
{
if (IsOwner ())
{
ListElement<Object*> const* ptr;
for (ptr = list.Head(); ptr != 0; ptr = ptr->Next ())
delete ptr->Datum ();
}
list.Purge ();
count = 0;
}
StackAsLinkedList::~StackAsLinkedList ()
{
Purge ();
}
void StackAsLinkedList::Push (Object& object)
{
list.Prepend (&object);
++count;
}
Object& StackAsLinkedList::Pop ()
{
if (count == 0)
throw domain_error ("stack is empty");
Object& const result = *list.First ();
list.Extract (&result);
--count;
return result;
}
Object& StackAsLinkedList::Top () const
{
if (count == 0)
throw domain_error ("stack is empty");
return *list.First ();
}
StackAsLinkedList::Iter::Iter (
StackAsLinkedList const& _stack) :
stack (_stack)
{ Reset (); }
bool StackAsLinkedList::Iter::IsDone () const
{ return position == 0; }
Object& StackAsLinkedList::Iter::operator * () const
{
if (position != 0)
return *position->Datum ();
else
return NullObject::Instance ();
}
void StackAsLinkedList::Iter::operator ++ ()
{
if (position != 0)
position = position->Next ();
}
void StackAsLinkedList::Iter::Reset ()
{ position = stack.list.Head (); }
inizializer列表不会识别列表
StackAsLinkedList::StackAsLinkedList () : list ()
我得到的一些错误是:注意:第一个错误将我带到原来的问题:
LinkedList<Object*> list;
error C2143: syntax error : missing ';' before '<'
error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2238: unexpected token(s) preceding ';'
error C2433: 'StackAsLinkedList::Iter::position' : 'virtual' not permitted on data declarations
error C2614: 'StackAsLinkedList' : illegal member initialization: 'list' is not a base or member
error C2065: 'list' : undeclared identifier
error C2228: left of '.Head' must have class/struct/union
error C2065: 'list' : undeclared identifier
error C2228: left of '.Purge' must have class/struct/union
答案 0 :(得分:1)
这听起来像StackAsLinkedList类不可见LinkedList类。在“StackAsLinkedList.h”的顶部包含“LinkedList.h”