如何使用 C++ 中定义的结构为 class 构造头文件
How to contruct header file for class with struct define inside in C++
如果我有 class 其中包含一个结构。如何在头文件中声明该结构?
参见下面的示例。这是正确的语法吗?
上下文:我的教授有一个 IList.h 文件,我们必须在 LinkedList class 中实现它。这就是我的示例中存在继承语法的原因。
LinkedList.cpp
#include "LinkedList.h"
class LinkedList : public IList {
struct Node
{
int data;
struct Node *next;
}
};
LinkedList.h
#ifndef LINKED_LIST_
#define LINKED_LIST_
#include "IList.h"
class LinkedList: public IList
{
protected:
struct Node;
};
IList.h
// Modified from code created by Frank M. Carrano and Timothy M. Henry.
// Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.
#ifndef I_LIST_
#define I_LIST_
class IList
{
public:
/** Constructor */
IList () : traverseCount(0) { }
/** Destroys object and frees memory allocated by object.
(See C++ Interlude 2) */
virtual ~IList () { }
/** Gets the current number of entries in this list.
@return The integer number of entries currently in the list. */
virtual int getCurrentSize() const = 0;
/** Sees whether this list is empty.
@return True if the list is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to this list.
@post If successful, newEntry is stored in the list and
the count of items in the list has increased by 1.
@param newEntry The object to be added as a new entry.
@return True if addition was successful, or false if not. */
virtual bool add(int newEntry) = 0;
/** Removes one occurrence of a given entry from this list,
if possible.
@post If successful, anEntry has been removed from the list
and the count of items in the list has decreased by 1.
@param anEntry The entry to be removed.
@return True if removal was successful, or false if not. */
virtual bool remove(int anEntry) = 0;
/** Removes all entries from this list.
@post List contains no items, and the count of items is 0. */
virtual void clear() = 0;
/** Tests whether this list contains a given entry.
@param anEntry The entry to locate.
@return True if list contains anEntry, or false otherwise. */
virtual bool contains(int anEntry) = 0;
/** Get the count of number of nodes traversed.
@return The integer number of nodes traversed since last time the count was reset. */
virtual int getTraverseCount() const { return traverseCount; }
/** Reset the count of nodes traversed to zero. */
virtual void resetTraverseCount() { traverseCount = 0; }
protected:
int traverseCount;
}; // end IList
#endif
您至少需要在 LinkedList
class 中重新声明来自 IList
的纯虚拟方法,否则 LinkedList
将是抽象的 class 所以编译器不允许你实例化一个 LinkedList
对象:
// LinkedList.h
#ifndef LINKED_LIST_
#define LINKED_LIST_
#include "IList.h"
class LinkedList: public IList
{
public:
LinkedList();
virtual ~LinkedList();
virtual int getCurrentSize() const;
virtual bool isEmpty() const;
virtual bool add(int newEntry);
virtual bool remove(int anEntry);
virtual void clear();
virtual bool contains(int anEntry);
private:
struct Node
{
int data;
struct Node *next;
};
struct Node *first;
};
#endif
...那么您需要在 LinkedList.cpp:
中为您的方法编写实际代码
// LinkedList.cpp
#include "LinkList.h"
LinkedList :: LinkedList()
{
[... code goes here...]
}
LinkedList :: ~LinkedList()
{
[... code goes here...]
}
int LinkedList :: getCurrentSize() const
{
[... code goes here...]
}
bool LinkedList :: isEmpty() const
{
[... code goes here...]
}
bool LinkedList :: add(int newEntry)
{
[... code goes here...]
}
bool LinkedList :: remove(int anEntry)
{
[... code goes here...]
}
void LinkedList :: clear()
{
[... code goes here...]
}
bool LinkedList :: contains(int anEntry)
{
[... code goes here...]
}
如果我有 class 其中包含一个结构。如何在头文件中声明该结构?
参见下面的示例。这是正确的语法吗?
上下文:我的教授有一个 IList.h 文件,我们必须在 LinkedList class 中实现它。这就是我的示例中存在继承语法的原因。
LinkedList.cpp
#include "LinkedList.h"
class LinkedList : public IList {
struct Node
{
int data;
struct Node *next;
}
};
LinkedList.h
#ifndef LINKED_LIST_
#define LINKED_LIST_
#include "IList.h"
class LinkedList: public IList
{
protected:
struct Node;
};
IList.h
// Modified from code created by Frank M. Carrano and Timothy M. Henry.
// Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.
#ifndef I_LIST_
#define I_LIST_
class IList
{
public:
/** Constructor */
IList () : traverseCount(0) { }
/** Destroys object and frees memory allocated by object.
(See C++ Interlude 2) */
virtual ~IList () { }
/** Gets the current number of entries in this list.
@return The integer number of entries currently in the list. */
virtual int getCurrentSize() const = 0;
/** Sees whether this list is empty.
@return True if the list is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Adds a new entry to this list.
@post If successful, newEntry is stored in the list and
the count of items in the list has increased by 1.
@param newEntry The object to be added as a new entry.
@return True if addition was successful, or false if not. */
virtual bool add(int newEntry) = 0;
/** Removes one occurrence of a given entry from this list,
if possible.
@post If successful, anEntry has been removed from the list
and the count of items in the list has decreased by 1.
@param anEntry The entry to be removed.
@return True if removal was successful, or false if not. */
virtual bool remove(int anEntry) = 0;
/** Removes all entries from this list.
@post List contains no items, and the count of items is 0. */
virtual void clear() = 0;
/** Tests whether this list contains a given entry.
@param anEntry The entry to locate.
@return True if list contains anEntry, or false otherwise. */
virtual bool contains(int anEntry) = 0;
/** Get the count of number of nodes traversed.
@return The integer number of nodes traversed since last time the count was reset. */
virtual int getTraverseCount() const { return traverseCount; }
/** Reset the count of nodes traversed to zero. */
virtual void resetTraverseCount() { traverseCount = 0; }
protected:
int traverseCount;
}; // end IList
#endif
您至少需要在 LinkedList
class 中重新声明来自 IList
的纯虚拟方法,否则 LinkedList
将是抽象的 class 所以编译器不允许你实例化一个 LinkedList
对象:
// LinkedList.h
#ifndef LINKED_LIST_
#define LINKED_LIST_
#include "IList.h"
class LinkedList: public IList
{
public:
LinkedList();
virtual ~LinkedList();
virtual int getCurrentSize() const;
virtual bool isEmpty() const;
virtual bool add(int newEntry);
virtual bool remove(int anEntry);
virtual void clear();
virtual bool contains(int anEntry);
private:
struct Node
{
int data;
struct Node *next;
};
struct Node *first;
};
#endif
...那么您需要在 LinkedList.cpp:
中为您的方法编写实际代码// LinkedList.cpp
#include "LinkList.h"
LinkedList :: LinkedList()
{
[... code goes here...]
}
LinkedList :: ~LinkedList()
{
[... code goes here...]
}
int LinkedList :: getCurrentSize() const
{
[... code goes here...]
}
bool LinkedList :: isEmpty() const
{
[... code goes here...]
}
bool LinkedList :: add(int newEntry)
{
[... code goes here...]
}
bool LinkedList :: remove(int anEntry)
{
[... code goes here...]
}
void LinkedList :: clear()
{
[... code goes here...]
}
bool LinkedList :: contains(int anEntry)
{
[... code goes here...]
}