gpt4 book ai didi

c++ - 无法将字段 声明为抽象类型
转载 作者:塔克拉玛干 更新时间:2023-11-03 07:25:36 25 4
gpt4 key购买 nike

这是我收到的错误,需要帮助解决...

g++ -g -c -std=c++11 main.cpp
In file included from main.cpp:3:0:
Executive.h:18:25: error: cannot declare field 'Executive::queue' to be of abstract type 'ListQueue<std::basic_string<char> >'
ListQueue<std::string> queue;
^
In file included from Executive.h:12:0,
from main.cpp:3:
ListQueue.h:8:7: note: because the following virtual functions are pure within 'ListQueue<std::basic_string<char> >':
class ListQueue : public QueueInterface<ItemType>
^

我不习惯使用“虚拟”类甚至 C++ 来解决这个问题,仍在学习,但我在编译我的代码时遇到了问题,就像现在这样。虚拟类(class)由讲师提供,因此我将发布我的 Executive、ListQueue 和 ArrayStack 类(class)的头文件。 ListQueue 和 ArrayStack 是与其“虚拟”QueueInterface 和 StackInterface 类相关的类。

执行.h

    #include <iostream>
#include <string>
#include <fstream>

#include "ListQueue.h"
#include "ArrayStack.h"

class Executive
{
private:
ListQueue<std::string> queue;
ArrayStack<std::string> stack;
std::string serving = "no one";
std::string waiting = "no one";
bool isWaiting = false;
bool wasVIP = false;
bool servingVIP = false;
public:

/** Constructor reads in input file */
Executive(std::istream& inputFile);

/** Reads in and interprets the text file */
void read(std::istream& is);

/** Shows that the person currently being served finishes and the person waiting begins */
void done();

/** Shows who is currently being served and who is next */
void show();
};

列表队列.h

 #include "QueueInterface.h"
#include "Node.h"

template<class ItemType>
class ListQueue : public QueueInterface<ItemType>
{
private:
Node<ItemType>* first; //front of queue
Node<ItemType>* last; //end of queue

public:
ListQueue(); // Default constructor
bool isEmpty() const = 0;
void enqueue(const ItemType& newEntry) throw (PrecondViolatedExcep) = 0;
void dequeue() throw (PrecondViolatedExcep) = 0;
ItemType peekFront() const throw (PrecondViolatedExcep) = 0;
}; // end ListQueue

数组堆栈

    #include "StackInterface.h"

const int MAX_STACK = 10;

template<class ItemType>
class ArrayStack : public StackInterface<ItemType>
{
private:
ItemType items[MAX_STACK]; // Array of stack items
int top; // Index to top of stack

public:
ArrayStack(); // Default constructor
bool isEmpty() const;
void push(const ItemType& newEntry) throw (PrecondViolatedExcep);
void pop() throw (PrecondViolatedExcep);
ItemType peek() const throw (PrecondViolatedExcep);
}; // end ArrayStack

堆栈接口(interface)

template<typename ItemType>
class StackInterface
{
public:
/** Sees whether this stack is empty.
@return True if the stack is empty, or false if not. */
virtual bool isEmpty() const = 0;

/** Adds a new entry to the top of this stack.
@pre a push is possible
@post If the operation was successful, newEntry is at the top of the stack.
@param newEntry The object to be added as a new entry. */
virtual void push(const ItemType& newEntry)
throw (PrecondViolatedExcep) = 0;

/** Removes the top of this stack.
@pre The stack is not empty.
@post If the operation was successful, the top of the stack
has been removed. */
virtual void pop()
throw (PrecondViolatedExcep) = 0;

/** Returns the top of this stack.
@pre The stack is not empty.
@post The top of the stack has been returned, and
the stack is unchanged.
@return The top of the stack. */
virtual ItemType peek() const throw (PrecondViolatedExcep) = 0;
}; // end StackInterface

队列接口(interface)

template<typename ItemType>
class QueueInterface
{
public:
/** Sees whether this queue is empty.
@return True if the queue is empty, or false if not. */
virtual bool isEmpty() const = 0;

/** Adds a new entry to the back of this queue.
@pre an enqueue is possible
@post If the operation was successful, newEntry is at the
back of the queue.
@param newEntry The object to be added as a new entry. */
virtual void enqueue(const ItemType& newEntry)
throw (PrecondViolatedExcep) = 0;

/** Removes the front of this queue.
@pre The queue is not empty.
@post If the operation was successful, the front of the queue
has been removed. */
virtual void dequeue()
throw (PrecondViolatedExcep) = 0;

/** Returns the front of this queue.
@pre The queue is not empty.
@post The front of the queue has been returned, and the
queue is unchanged.
@return The front of the queue. */
virtual ItemType peekFront() const
throw (PrecondViolatedExcep) = 0;
}; // end QueueInterface

最佳答案

您不能将队列声明为 List 类型,您可以将其设为指向抽象类型 ListQueue 的指针。与堆栈相同。

class Executive
{
private:
ListQueue<std::string> *queue;
ArrayStack<std::string> *stack;
std::string serving = "no one";
std::string waiting = "no one";
bool isWaiting = false;
bool wasVIP = false;
bool servingVIP = false;
public:
}

你不能这样做的原因是如果它不是一个指针,编译器必须确定你的新类在内存中的形状,它需要知道你的类定义中的字段类型的大小是。但是我不能为抽象类这样做,因为它不是一个完整的定义。如果您将它设为指向它可以识别的抽象类的指针,因为指针只是一个整数。


与您交谈后,我意识到了错误。您正在继承老师给您的接口(interface)。这是一个工具,当你编写代码来给其他人一份契约(Contract),契约(Contract)说给我 build 一些东西来做我想做的事情,这就是我使用它的方式。

你的教授给你的接口(interface)定义是空的,没有定义函数应该做什么的逻辑。你的类应该继承他的接口(interface),你已经完成了,但是你的类也应该填写所有方法的细节。

例如:

#include "QueueInterface.h"
#include "Node.h"

template<class ItemType>
class ListQueue : public QueueInterface<ItemType>
{
private:
Node<ItemType>* first; //front of queue
Node<ItemType>* last; //end of queue

public:
ListQueue(); // Default constructor
bool isEmpty() const
{
// add logic here that will determine if the queue is empty.
}

void enqueue(const ItemType& newEntry) throw (PrecondViolatedExcep)
{
// Add logic to add element to queue
}

void dequeue() throw (PrecondViolatedExcep)
{
// add logic to remove element from queue
}

ItemType peekFront() const throw (PrecondViolatedExcep)
{
// add logic to look at next element in queue without removing it.
}

}; // end ListQueue

一旦您为这些接口(interface)填写了逻辑,您的类现在就已经完全定义好了,您可以使用它了。你不限于那些你可以添加辅助方法的方法,如果你喜欢和构造函数,析构函数等。但你必须至少实现作为接口(interface)一部分的集合。

有道理吗?

这有道理吗。

关于c++ - 无法将字段 <Object name> 声明为抽象类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19780949/

25 4 0