我在vc6新建空win32应用程序,新建类,然后编译的时候vc说不认是NULL空指针

解决方案 »

  1.   

    你新建了类,从CObject继承下来的吗?如果没有,可能要在其头文件开始部分添加
    #include "stdafx.h"
      

  2.   

    那你用0代替呀。
    int *p=0;
      

  3.   

    // ListNode.cpp: implementation of the ListNode class.
    //
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Construction/Destruction
    //////////////////////////////////////////////////////////////////////
    enum Boolean {False,True}; 
    class List;
    class ListIterator;class ListNode  
    {
    friend class List;
    friend class ListIterator;
    public:
    ListNode();
    virtual ~ListNode();
    ListNode(const char value):data(value),link(NULL){};
    ListNode(const char& item);
    ListNode * NextNode(){return link;}
    void InsertAfter(ListNode *p);
    ListNode * GetNode(const char & item,ListNode * next);
    ListNode * RemoveAfter();
    private:
    char data;
    ListNode * link;};class List{
    public:
    List(const char & value) {last=first=new ListNode(value);};//构造函数
    ~List();
    void MakeEmpty();
    int Length() const;
    ListNode *Find(char value);
    ListNode * Find(int i);
    int Insert(char value,int i);  //将新元素插入到i位置
    char * Remove(int i);
    char * Get(int i);
    private:
    ListNode * first,last;
    };
    class ListIterator{
    public:
    ListIterator(const List & l):list(l),current(l.first){};
    Boolean NotNull();
    Boolean NextNotNull();
    char * First();
    char * Next();
    private:
    const List & list;
    ListNode * current;
    }ListNode::ListNode():link(NULL){}ListNode::~ListNode()
    {}ListNode::ListNode(const char & item):data(item),link(NULL){
    }void ListNode::InsertAfter(ListNode *p){
    //将p所指的节点链接成当前节点的后继节点
    p->link=link;
    link=p;
    }ListNode * ListNode::GetNode(const char & item,ListNode * next=NULL){
    //以item和next建立一个新节点,并返回其地址
    ListNode *newnode=new ListNode(item);
    newnode->link=next;
    newnode->link=next;
    return newnode;
    }ListNode * ListNode::RemoveAfter(){
    //从链中摘下当前的下一个节点,返回其地址
    ListNode * temp=link;
    if (link==NULL) return NULL;
    link=temp->link;
    return temp;
    }
    //下面是List类List::~List(){
    MakeEmpty();
    delete first;
    }
    void List::MakeEmpty(){
    //将链表置为空表
    ListNode *q;
    while (first->link!=NULL){
    q=first->link;
    first->link=q->link;
    delete q;
    }
    last=first;
    }int List::Length() const {
    ListNode * p=first->link;
    int count=0;
    while (p!=NULL){
    p=p->link;
    count++;
    }
    return count;}ListNode * List::Find(char value){
    //
    ListNode *p=first->link;
    while(p!=NULL && p->data!=value)
    p=p->link;
    return p;}
    ListNode * List::Find(int i){
    if (i<-1) return NULL:
    if (i==-1) return first;
    ListNode * p=first->link;
    int j=0;
    while (p!=NULL && j<i){
    p=p->link;
    j++;
    }
    return p;
    }int List::Insert(char value,int i){
    //将新元素value插入到i,成功则返回1,否返回0
    ListNode *p=Find(i-1);
    if (p==NULL) return 0;
    ListNode * newnode=GetNode(value,p->link);
    if (p->link==NULL) last=newnode;
    p->link=newnode;
    return 1;
    }
    char * List::Remove(int i)
    {
    ListNode *p=Find(i-1),*q;
    if (p==NULL||p->link==NULL) return NULL:
    q=p->link;
    p->link=q->link;
    char value=q->data;
    if (q==last) last=p;
    delete q;
    return & value;
    }char *  List::Get(int i)
    {
    ListNode *p=Find(i);
    if (p==NULL||p==first) return NULL;
    else return &p->data;
    }Boolean ListIterator::NotNull()
    {
    if (current!=NULL) return True;
    else return False;
    }
    Boolean ListIterator::NextNotNull()
    {
    if (current!=NULL&& current->link!=NULL) return True;
    else return False;
    }
    char * ListIterator::First()
    {
    if (list.first->link!=NULL) 
    {
    current=list.first;
    return & current->data; }
    else
    {
    current=NULL;
    return NULL;
    }
    }char * ListIterator::Next()
    {
    if (current!=NULL && current->link!=NULL){
    current=current->link;
    return & current->data; }
    else
    {
    current=NULL;
    return NULL;
    }
    }int main()
    {
    /*     */
    return 1;
    }
      

  4.   

    那个太多bug了,看这个把,错误一样
    class Stack;
    class Node
    {
    friend class Stack;
    public:

    Node(char d=0,Node * l=NULL):data(d),link(l){}
    private:
    char data;   //节点数据域
    Node * link;  //指针域
    };class Stack
    {
    public:
    Stack():top(NULL){};
    ~Stack();
    void Push(const char & item);
    char Pop();
    char GetTop();
    void MakeEmpty();
    int IsEmpty() const {return top==NULL;}
    private:
    Node * top;
    };Stack::~Stack()
    {
    if (!IsEmpty()) MakeEmpty();
    }void Stack::MakeEmpty()
    {
    Node *p;
    while(top!=NULL){
    p=top;
    top=top->link;
    delete p;
    }
    }void Stack::Push(const char &item){
    top=new Node(item,top);
    }char Stack::Pop()
    {
    //删除栈顶元素,并返回她的值
    if (IsEmpty() ) return 0;
    Node * p=top;
    char retvalue=p->data;
    top=top->link;
    delete p;
    return retvalue;
    }char Stack::GetTop()
    {
    if (IsEmpty()) return 0;
    return top->data;
    }int main()
    {
    return 1;
    }
    出错
    ompiling...
    Expr.cpp
    E:\MyDoc\c++\Expr\Expr.cpp(9) : error C2065: 'NULL' : undeclared identifier
    E:\MyDoc\c++\Expr\Expr.cpp(9) : error C2440: 'default argument' : cannot convert from 'int' to 'class Node *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    E:\MyDoc\c++\Expr\Expr.cpp(37) : error C2446: '!=' : no conversion from 'int' to 'class Node *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    E:\MyDoc\c++\Expr\Expr.cpp(37) : error C2040: '!=' : 'class Node *' differs in levels of indirection from 'int'
    Error executing cl.exe.Expr.exe - 4 error(s), 0 warning(s)
      

  5.   


    加如下的预编译语句#ifndef NULL
    #define NULL  0
    #endif
      

  6.   

    我估计是你自己定义了null!和系统定义的冲突哦!
    楼上方法可以解决!
      

  7.   

    加如下的预编译语句#ifndef NULL
    #define NULL  0
    #endif
    NULL 就是0