链接的对象列表?

时间:2010-11-02 22:16:33

标签: c++ data-structures compiler-errors

我有一个程序正在使用带有数据结构的adt / abstract数据类。 对象 - > Ownership->容器 - >堆栈 - >队列 - > queueslinkedlist等。 我从视觉工作室切换到代码块,因为我遇到了ostream问题..

我评论了一些功能,以便追查问题..

我想让它编译。在queueaslinklist.h文件中我遇到了问题。

class QueueAsLinkedList : public virtual Queue
{
protected:
LinkedList<Object*> list;

当我在.cpp中注释掉这个和初始化列表时,程序将编译,所以我认为我的问题缩小了。不知道为什么虽然..

我得到的错误是:

  

obj \ Debug \ QueueAsLinkedList.o(。text + 0x198)||在函数`ZN17QueueAsLinkedListD2Ev'中:       \ QueueAsLinkedList.cpp | 10 |未定义引用`LinkedList ::〜LinkedList()'|        QueueAsLinkedList.o(。text + 0x394)||在函数`ZN17QueueAsLinkedListD1Ev'中:       \ QueueAsLinkedList.cpp | 10 |未定义引用`LinkedList ::〜LinkedList()'|       QueueAsLinkedList.o(。text + 0x598)||在函数`ZN17QueueAsLinkedListD0Ev'中:       \ QueueAsLinkedList.cpp | 10 |未定义引用`LinkedList ::〜LinkedList()'|       QueueAsLinkedList.o(.text + 0x7b4)||在函数`ZN17QueueAsLinkedListC2Ev'中:       \ QueueAsLinkedList.cpp | 16 |未定义引用`LinkedList :: LinkedList()'|       \ QueueAsLinkedList.o(。text + 0x8cb)||在函数`ZN17QueueAsLinkedListC1Ev'中:       \ QueueAsLinkedList.cpp | 16 |未定义引用`LinkedList :: LinkedList()'|       || ===构建完成:5个错误,0个警告=== |

以下是我认为你需要看到的代码..我将包括object - container - 和linklist que

将队列列为链接列表.h

#ifndef QUEUEASLINKEDLIST_H
#define QUEUEASLINKEDLIST_H

#include "Queue.h"
#include "Object.h"
#include "LinkedList.h"



class QueueAsLinkedList : public virtual Queue
{
protected:
LinkedList<Object*> list;


public:
QueueAsLinkedList ();
~QueueAsLinkedList ();

//void QueueAsLinkedList::Purge ();
//Object& QueueAsLinkedList::Head () const;
//void QueueAsLinkedList::Enqueue (Object& Object);
//Object& QueueAsLinkedList::Dequeue ();
};

#endif

将队列列为链接列表.cpp

    #include "QueueAsLinkedList.h"
#include "Ownership.h"
#include <iostream>

using namespace std;

QueueAsLinkedList::~QueueAsLinkedList()
{


}

QueueAsLinkedList::QueueAsLinkedList () : list ()
{
}

目标文件

#ifndef OBJECT_H
#define OBJECT_H
#include <iostream>


class Object
{
protected:
virtual int CompareTo (Object const&) const = 0;
public:
virtual ~Object ();
virtual bool IsNull () const;
virtual int Compare (Object const&) const;
//  virtual HashValue Hash () const = 0;
virtual void Put (std::ostream&) const = 0;
};

#endif


}

对象.cpp

#include  <typeinfo>
#include  "Object.h"


Object::~Object ()
{}

bool Object::IsNull () const
{ return false; }

int Object::Compare (Object const& object) const
{
if (typeid (*this) == typeid (object))
return CompareTo (object);
else if (typeid (*this).before (typeid (object)))
return -1;
else
return 1;
}

inline bool operator == (Object const& left, Object const& right)
{ return left.Compare (right) == 0; }

inline bool operator != (Object const& left, Object const& right)
{ return left.Compare (right) != 0; }

inline bool operator <= (Object const& left, Object const& right)
{ return left.Compare (right) <= 0; }

inline bool operator <  (Object const& left, Object const& right)
{ return left.Compare (right) <  0; }

inline bool operator >= (Object const& left, Object const& right)
{ return left.Compare (right) >= 0; }

inline bool operator >  (Object const& left, Object const& right)
{ return left.Compare (right) >  0; }

inline std::ostream& operator << (std::ostream &s, Object const &object)
{ object.Put (s); return s; }

她是链接列表

template <class T>
class LinkedList;

template <class T>
class ListElement
{
T datum;
ListElement* next;

ListElement (T const&, ListElement*);
public:
T const& Datum () const;
ListElement const* Next () const;

friend class    LinkedList<T>; 
};

template <class T>
class LinkedList    
{
ListElement<T>* head;
ListElement<T>* tail;
public:
LinkedList ();
~LinkedList ();

LinkedList (LinkedList const&);
LinkedList& operator = (LinkedList const&);

ListElement<T> const* Head () const;
ListElement<T> const* Tail () const;
bool IsEmpty () const;
T const& First () const;
T const& Last () const;

void Prepend (T const&);
void Append (T const&);
void Extract (T const&);
void Purge ();
void InsertAfter (ListElement<T> const*, T const&);
void InsertBefore (ListElement<T> const*, T const&);
};

link list.cpp

#include "LinkedList.h"


template <class T>
ListElement<T>::ListElement (
T const& _datum, ListElement<T>* _next) :
datum (_datum), next (_next)
{}

template <class T>
T const& ListElement<T>::Datum () const
{ return datum; }

template <class T>
ListElement<T> const* ListElement<T>::Next () const
{ return next; }



template <class T>
LinkedList<T>::LinkedList () :
head (0),
tail (0)
{}


template <class T>
void LinkedList<T>::Purge ()
{
while (head != 0)
{
ListElement<T>* const tmp = head;
head = head->next;
delete tmp;
}
tail = 0;
}

template <class T>
LinkedList<T>::~LinkedList ()
{ Purge (); }



template <class T>
ListElement<T> const* LinkedList<T>::Head () const
{ return head; }

template <class T>
ListElement<T> const* LinkedList<T>::Tail () const
{ return tail; }

template <class T>
bool LinkedList<T>::IsEmpty () const
{ return head == 0; }



template <class T>
T const& LinkedList<T>::First () const
{
if (head == 0)
//  throw domain_error ("list is empty");
return head->datum;
}

template <class T>
T const& LinkedList<T>::Last () const
{
if (tail == 0)
//throw domain_error ("list is empty");
return tail->datum;
}



template <class T>
void LinkedList<T>::Prepend (T const& item)
{
ListElement<T>* const tmp = new ListElement<T> (item, head);
if (head == 0)
tail = tmp;
head = tmp;
}

这是容器

#ifndef CONTAINER_H
#define CONTAINER_H

#include <iostream>
#include "Object.h"

#include "NullObject.h"
#include "Ownership.h"
#include "Iterator.h"
#include "Visitor.h"



class Container : public virtual Object,    public virtual Ownership
{
protected:
unsigned int count;

Container ();
public:
virtual unsigned int Count () const;
virtual bool IsEmpty () const;
virtual bool IsFull () const;
//  virtual HashValue Hash () const;
//virtual void Put (ostream&) const;
virtual Iterator& NewIterator () const;

virtual void Purge () = 0;
virtual void Accept (Visitor&) const = 0;
};

#endif

这里是container.cpp

#include <iostream>
#include "Container.h"
#include "NullIterator.h"



Container::Container () :
count (0)
{}

unsigned int Container::Count () const
{ return count; }

bool Container::IsEmpty () const
{ return Count () == 0; }

bool Container::IsFull () const
{ return false; }

Iterator& Container::NewIterator () const
{ return *new NullIterator (); }

//void Container::Put (ostream&) const
//{ return  ;}

和que.h

#ifndef QUEUE_H
#define QUEUE_H

#include "Container.h"


class Queue : public virtual Container
{
public:
virtual Object& Head () const = 0;
virtual void Enqueue (Object&) = 0;
virtual Object& Dequeue () = 0;
};


#endif

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

将包含LinkedList<T>的类模板的代码移动到头文件中 - 没有单独的.cpp文件。

这通常是模板的工作原理,应该简化在编译和链接中实例化模板的任务。如果您这样做,QueueAsLinkedList中的用法应该意味着使用特定值T(在这种情况下为Object*)进行实例化,并且链接将起作用。

这里的提示是你的链接器错误都引用了LinkedList的默认构造函数和析构函数。虽然您已在linkedlist.cpp中为每个类型定义了实现,但仍必须为要用于T的每个特定类型实例化类模板。您可以明确地执行此操作(这是解决问题的另一种方法),或者正如我所建议的那样。

当您不熟悉课程模板时,这非常令人困惑 - 讨论了主题here

答案 1 :(得分:0)