我尝试编译此代码时遇到此结构构造函数的问题:
typedef struct Node
{
Node( int data ) //
{
this->data = data;
previous = NULL; // Compiler indicates here
next = NULL;
}
int data;
Node* previous;
Node* next;
} NODE;
Run Code Online (Sandbox Code Playgroud)
当我来这个错误发生时:
\linkedlist\linkedlist.h||In constructor `Node::Node(int)':|
\linkedlist\linkedlist.h|9|error: `NULL' was not declared in this scope|
||=== Build finished: 1 errors, 0 warnings ===|
Run Code Online (Sandbox Code Playgroud)
最后一个问题是结构,但它在我的main.cpp中运行正常,这次是在头文件中并且给我这个问题.我正在使用Code :: Blocks来编译此代码
我隐约记得几年前读过这篇文章,但我在网上找不到任何参考.
你能给我一个NULL宏没有扩展到0的例子吗?
编辑清晰:今天,它扩展为((void *)0),(0)或(0L).但是,有些架构早已被遗忘,而这种情况并非如此,并且NULL扩展到了不同的地址.就像是
#ifdef UNIVAC
#define NULL (0xffff)
#endif
Run Code Online (Sandbox Code Playgroud)
我正在寻找这样一台机器的例子.
更新以解决问题:
我不是在现行标准的背景下提出这个问题,也不是用不正确的术语来扰乱人们.但是,我接受的答案证实了我的假设:
后来的模型使用了[blah],显然是对所有现存的写得不好的C代码的反应,这些代码做出了错误的假设.
有关当前标准中的空指针的讨论,请参阅此问题.
我有以下结构:
struct node {
int data;
struct node *next;
};
struct node *n1 = malloc(sizeof(struct node));
Run Code Online (Sandbox Code Playgroud)
我不确定如何将struct指针的所有指针字段初始化为NULL而不导致任何潜在的内存泄漏?
我知道NULL是#defined为0.它似乎是一个int转换为指针类型的常量.那么当有两个重载函数时会发生什么:一个采用指针类型,另一个采用int类型.这是如何工作的nullptr?
#include <iostream>
using namespace std;
void callMe(int* p)
{
cout << "Function 1 called" << endl;
}
void callMe(int i)
{
cout << "Function 2 called" << endl;
}
int main()
{
callMe(nullptr);
callMe(NULL);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我知道callMe(nullptr)肯定会调用第一个函数.但是哪个函数会被调用callMe(NULL)?我在我的电脑上编译了这段代码.只有一个警告.
g++ -std=c++11 nullptr_type.cpp
nullptr_type.cpp: In function 'int main()':
nullptr_type.cpp:44:14: warning: passing NULL to non-pointer argument 1 of 'void callMe(int)' [-Wconversion-null]
callMe(NULL);
^
Run Code Online (Sandbox Code Playgroud)
我的代码编译没有任何问题,当我运行它时,我看到callMe(NULL)称为函数2. Visual Studio也编译代码,它调用函数2.
但是,当我尝试在在线GeeksForGeeks IDE上编译我的代码时,我遇到了一些编译器错误.
https://ide.geeksforgeeks.org/UsLgXRgpAe
我想知道为什么会出现这些错误. …