C++ 类中定义的指针的问题

源代码如下:
TNode.h源代码:
class TNode
{
public:
bool visited;
short name;
TNode *parent,*child,*nextsibling;

TNode();
TNode(short name);
~TNode();
};
Tnode.cpp源代码如下:
#include "TNode.h"
TNode::TNode()
{
visited = false;
name = 0;
parent = NULL;
child = NULL;
nextsibling = NULL;
}

TNode::TNode(short name)
{
visited = false;
name = name;
parent = NULL;
child = NULL;
nextsibling = NULL;
}

TNode::~TNode ()
{

}
结果编译的时候报错 报的是:
d:\cpp\test\cube0227\tnode.cpp(6) : error C2065: 'NULL' : undeclared identifier
d:\cpp\test\cube0227\tnode.cpp(6) : error C2440: '=' : cannot convert from 'int' to 'class TNode *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
d:\cpp\test\cube0227\tnode.cpp(7) : error C2440: '=' : cannot convert from 'int' to 'class TNode *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
d:\cpp\test\cube0227\tnode.cpp(8) : error C2440: '=' : cannot convert from 'int' to 'class TNode *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
d:\cpp\test\cube0227\tnode.cpp(15) : error C2440: '=' : cannot convert from 'int' to 'class TNode *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
d:\cpp\test\cube0227\tnode.cpp(16) : error C2440: '=' : cannot convert from 'int' to 'class TNode *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
d:\cpp\test\cube0227\tnode.cpp(17) : error C2440: '=' : cannot convert from 'int' to 'class TNode *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

就是说我的那个NULL好像是没定义 这个怎么处理呢 小弟初学C++请各位大侠指点 我的QQ:494135969 要是哪位想一块学的话可以交流一下

为什么编译的时候总是报错:“NULL is undeclared identifier”楼主tdxue(ILanLan)2003-06-12 10:54:53 在 C/C++ / C语言 提问
我在VC下的.cpp文件中定义了一个类:

class point
{
private:
int *p;
public:
void set()
{
p=NULL;
}
};

但是编译的时候报错:error C2065: 'NULL' : undeclared identifier
想不通这个道理。
有劳解答!

问题点数:10、回复次数:7
Top

1 楼chuyangguangshin(阳光)回复于 2003-06-12 11:24:40 得分 4NULL根根本就没有定义啊,C++怎么知道它是什么呢?所以在程序开头加上:

#define NULL 0
Top

2 楼onion_535(因个懒惰)回复于 2003-06-12 11:26:19 得分 0 直接用0嘛
Top

3 楼Areece(Keen on Guru)回复于 2003-06-12 11:49:45 得分 6包括头文件,肯定有文件定义了这个东西的,而且有很多文件定义了他。
如果是指针的话,应该
#define NULL ((void *)0)
自己写也可以,如果不行的话就包括windows.h,肯定搞定。
Top

4 楼summerCsdn()回复于 2003-06-12 12:04:48 得分 0 #include <stdlib.h>
Top

5 楼bullet2003(bull)回复于 2003-06-12 13:06:19 得分 0 我认为用0比用NULL好,还是用0吧,也不用加一个头文件stdlib.h
Top

6 楼titainium(一千瓦的刀狼)回复于 2003-06-12 13:15:13 得分 0 NULL没有定义,你需要先声明。
Top

7 楼sandrowjw(我的小猫照片给弄坏了,心都碎了)回复于 2003-06-12 13:31:15 得分 0 用0。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-02-28
编译器报错是有道理的,确实是NULL没有定义,你必须在TNode.h文件的最前面加上:#include <iostream>
当然,如果你在主函数里还要用到cout,cin的话,需要再加上using namespace std;
主函数里不要再重复#include <iostream>了,不过别忘了include "TNode.h"。
第2个回答  2009-02-28
NULL 在标准的C++程序中,是没有定义的,NULL不是c++中的字符。
,VC是经过微软自己定义的一个开发工具,他为了形象,,将0用NULL来表示,,他在头文件中给你定义了,#define NULL 0 所以才可以用NULL来代表0

在你写控制台程序的时候,没有添加宏定义的头文件,编译器根本就不知道你的NULL是什么东西。想用NULL就自己定义宏,,不想还是改成0得了。。
第3个回答  2009-02-27
加个输入输出流的头文件即可。

#include<iostream.h>
第4个回答  2009-02-27
老兄 你少了一句话
#include "iostream.h"

加上试试
我QQ 446146442 一起讨论
相似回答