【发布时间】:2013-11-15 20:27:28
【问题描述】:
这是我收到的错误,需要帮助解决...
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++,我还在学习,但我无法像现在这样编译我的代码。虚拟课程由讲师提供,因此我将发布我的 Executive、ListQueue 和 ArrayStack 课程的头文件。 ListQueue 和 ArrayStack 是与其“虚拟”QueueInterface 和 StackInterface 类相关的类。
Executive.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();
};
ListQueue.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
ArrayStack.h
#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
堆栈接口
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
队列接口
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
【问题讨论】:
-
QueueInterface 中列出的所有方法也在 ListQueue 中定义。如果您愿意,我可以发布界面,但确实有任何变化。
-
ListQueue中的函数已声明但未定义(已实现)。声明后带有= 0的虚函数是纯虚函数,您不能使用纯虚函数实例化类。但是,您可以声明一个指向抽象类型的指针。将违规代码更改为ListQueue<std::string>* queue;是朝着正确方向迈出的第一步。
标签: c++ object virtual declaration abstract