set head to NULL ('NULL' : undeclared identifier)
Asked Answered
B

6

20

I defined a linked list in C++. I am trying to set a NULL value to the variable head (in the constructor of Movie_LinkedList), but I got:

movie.h(40): error C2065: 'NULL' : undeclared identifier

please note that I can't include any library except of iostream

#define NAME_LEN 100

class Movie_DataBase
{
private:
    long int m_Code;
    char m_Name[NAME_LEN];
    bool belongs_to_netwroks[2];    
public:
    Movie_DataBase();
    Movie_DataBase(char movie_name[],long int movie_code);
};


class Movie_LinkedList
{
private:
    class Movie_Node
    {
    public:
        Movie_DataBase* data;
        Movie_Node* prev;
        Movie_Node* next;
    public:
        Movie_Node();

    };
    Movie_Node* head; //pointer to the first node in the linkedlist
    int a;
public:
    void set_movie();
    Movie_LinkedList() { head = NULL; }

};

Any help appreciated!

Brainless answered 21/3, 2013 at 14:9 Comment(5)
That's because NULL hasn't been defined. Either define it or set head = 0Equerry
Why did you delete your code?Candiot
because there are students that can copy my code..Brainless
They still can; it's in the edit history.Ting
Woe to those students who copy broken code! :-PGlassworks
G
25

As written, NULL isn't defined in your program. Usually, that's defined in a standard header file -- specifically <cstddef> or <stddef.h>. Since you're restricted to iostream, if yours doesn't get NULL implicitly from that header, you can use 0 or, in C++11, nullptr, which is a keyword and doesn't require a header. (It is not recommended to define NULL yourself. It might work sometimes, but it is technically illegal.)

Glassworks answered 21/3, 2013 at 14:11 Comment(5)
including a header file for a macro is unnecessary. I am not the downvoter thoughLenhard
OP is not allowed to include headers!Lenhard
@Aniket The only way to get NULL is by including one of the standard headers which define it.Lent
that's certainly not true @JamesKanze the other way is posted below as my answerLenhard
@Aniket Your answer is wrong. It's illegal to define NULL in your code (at least if you include any standard headers).Lent
P
8

You should include <stddef.h> or <cstddef>.

However you can use 0 or nullptr too.

Perihelion answered 21/3, 2013 at 14:11 Comment(6)
I think nullptr is only for c++11Equerry
Op is not allowed to include headers. and assigning a pointer to an integer will cause compiler error.Lenhard
No it won't. Try int* p = 0; Very common.Glassworks
hmm @Glassworks try this: int *p = 0x1 - however the compiler says it is ok to assign 0 but not 1Lenhard
Thus spake the Creator: stroustrup.com/C++11FAQ.html#nullptr Note also the references.Glassworks
@Aniket That's because a constant integral expression evaluating to 0 is a null pointer constant, and converts implicitly to a null pointer of any type. How did you think NULL was defined?Lent
D
4

use the following include:

#include <stddef.h>
Datum answered 21/3, 2013 at 14:11 Comment(1)
@Aniket He has to, otherwise he cannot use NULL.Lent
L
2

No libraries needed!

on the top of the header file,

do this:

#ifndef NULL
#define NULL (0)
#endif
Lenhard answered 21/3, 2013 at 14:11 Comment(11)
This is forbidden by the standard, and results in undefined behavior.Lent
Worse: the definition you give isn't even legal.Lent
@JamesKanze it is legal nowLenhard
The problem was the difference between (void *)0 being NULL in C and 0 in C++ because of a certain incompatibilities.Lenhard
Provided it is in one of the standard headers. You're not allowed to provide definitions of things that are in the standard headers.Lent
but he is not including standard headers. And you can always undefine any macro(even in standard libraries)Lenhard
There's no real difference between traditional C and C++ here. NULL has always been 0, since the days of Kernighan and Richie. For some unknown reason, the C standards committee decided to allow an additional form, but it wasn't in K&R C, and certainly isn't traditional.Lent
let us continue this discussion in chatLenhard
<iostream> is a standard header. And while it is legal to undefine NULL, it's not legal for you to define it.Lent
See §17.6.4.3.1/1: "A translation unit that includes a standard library header shall not #define or #undef names declared in any standard library header."Lent
A proscription routinely violated, alas, when it comes to the min/max macros.Glassworks
T
1

NULL isn't actually part of the core C or C++ language; it's defined to be 0 in stddef.h

Since you're using C++, prefer nullptr, which is a keyword, and typed. (Assuming it's available. It's part of C++11, so technically not all compilers will support it; practically, you'd be hard-pressed to find a compiler that doesn't)

Ting answered 21/3, 2013 at 14:12 Comment(0)
S
0

I also encountered this. So need to add <string.h> headers.


#include<string.h>

typedef struct{
 node *head;
 node *current;
 int size
}list;

void appendList(list *l, int n){
  if(l->head == NULL){
    
  }
}

int main(){
 return 0;
}

I tried to create a linkedlist, and I encountered this.

Stylite answered 25/2 at 8:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.