【发布时间】:2011-11-24 09:18:23
【问题描述】:
我有一个 .h 文件,里面有这个:
#ifndef CS240_LINKED_LIST_H
#define CS240_LINKED_LIST_H
#include <string>
//! LLNode implements a doubly-linked list node
class LLNode {
friend class LinkedList;
public:
LLNode(const std::string & v, LLNode * p, LLNode * n) :
value(v), prev(p), next(n)
{
}
private:
std::string value; //!< value stored in the node
LLNode * prev; //!< pointer to previous node in the list
LLNode * next; //!< pointer to next node in the list
};
//! LinkedList implements a doubly-linked list
class LinkedList
{
public:
//! No-arg constructor. Initializes an empty linked list
LinkedList();
//! Copy constructor. Makes a complete copy of its argument
LinkedList(const LinkedList & other);
private:
//! two dummy nodes to keep track of the beginning and end of the list.
LLnode beginning;
LLnode end;
int size;
};
#endif
在我的 cpp 文件中:
#include "LinkedList.h"
LinkedList::LinkedList(){
this->beginning.prev = NULL;
this->beginning.next = this->end;
this->end.prev = this->beginning;
this->end.next = NULL;
}
这是输出:
>g++ -o LinkedList.o LinkedList.cpp
In file included from LinkedList.cpp:1:
LinkedList.h:37: error: 'LLnode' does not name a type
LinkedList.h:38: error: 'LLnode' does not name a type
LinkedList.cpp: In constructor 'LinkedList::LinkedList()':
LinkedList.cpp:4: error: 'class LinkedList' has no member named 'beginning'
LinkedList.cpp:5: error: 'class LinkedList' has no member named 'beginning'
LinkedList.cpp:5: error: 'class LinkedList' has no member named 'end'
LinkedList.cpp:6: error: 'class LinkedList' has no member named 'end'
LinkedList.cpp:6: error: 'class LinkedList' has no member named 'beginning'
LinkedList.cpp:7: error: 'class LinkedList' has no member named 'end'
我不知道如何解决这个问题。我还要如何设置开始和结束对象?请注意,我是一名学习 C++ 的 Java 程序员。
【问题讨论】:
-
不要在 C++ 中使用 NULL,如果您的编译器支持,请使用 0 或 nullptr。
-
这真的是你的标题吗?您需要在类定义中声明构造函数。
-
@Geoffroy:为什么在 C++ 中
0比NULL更好? (假设你没有上过C++11,也没有nullptr) -
@Geoffroy:
NULL应该可用(如果你想使用它)即使在最小的独立 C++ 实现中(来自<cstddef>)。如果您使用它,您可以记录您打算将哪些零作为空指针常量,哪些只是零。理论上,这应该更容易转移到nullptr。我同意0在 C++ 中是一个非常好的空指针常量,但我不同意在 C++ 中禁止使用NULL的有力论据。 -
@Geoffroy:你说过“不要在 C++ 中使用
NULL”。如果这不是禁令,我不知道是什么。我仍然不相信这只是 C++03 代码中的风格或约定问题。