gpt4 book ai didi

c++ - 对象的链表?

转载 作者:行者123 更新时间:2023-11-28 03:54:47 25 4
gpt4 key购买 nike

我有一个程序使用带有数据结构的 adt/抽象数据类。Object -> Ownership-> container -> stack -> queue -> queueslinkedlist 等。我从 visual studio 切换到代码块,因为我遇到了 ostream 问题..

我注释掉了一些功能以追踪问题..

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

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

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

我得到的错误是:

obj\Debug\QueueAsLinkedList.o(.text+0x198)||In function `ZN17QueueAsLinkedListD2Ev':| \QueueAsLinkedList.cpp|10|undefined reference to `LinkedList::~LinkedList()'| QueueAsLinkedList.o(.text+0x394)||In function `ZN17QueueAsLinkedListD1Ev':| \QueueAsLinkedList.cpp|10|undefined reference to `LinkedList::~LinkedList()'| QueueAsLinkedList.o(.text+0x598)||In function `ZN17QueueAsLinkedListD0Ev':| \QueueAsLinkedList.cpp|10|undefined reference to `LinkedList::~LinkedList()'| QueueAsLinkedList.o(.text+0x7b4)||In function `ZN17QueueAsLinkedListC2Ev':| \QueueAsLinkedList.cpp|16|undefined reference to `LinkedList::LinkedList()'| \QueueAsLinkedList.o(.text+0x8cb)||In function `ZN17QueueAsLinkedListC1Ev':| \QueueAsLinkedList.cpp|16|undefined reference to `LinkedList::LinkedList()'| ||=== Build finished: 5 errors, 0 warnings ===|

这是我认为您需要查看的代码。我将包括对象 - 容器 - 和链接列表问题

队列为链表.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&);
};

链接列表.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

非常感谢任何帮助。

最佳答案

移动类模板的代码,包括 LinkedList<T>进入头文件 - 没有单独的 .cpp 文件。

这是模板最常见的工作方式,应该可以简化在编译和链接中实例化模板的任务。如果你这样做,用法如 QueueAsLinkedList应该暗示具有特定值 T 的实例化(在本例中为 Object*),链接将有效。

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

当您不熟悉类模板的使用时,这会让人非常困惑 - 有一个关于主题 here 的讨论.

关于c++ - 对象的链表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4082584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com