wsl 上的 vscode 中的标识符 "NULL" 未定义且标识符 "strncpy" 未定义

identifier "NULL" is undefined and identifier "strncpy" is undefined in vscode on wsl

我正在尝试在 wsl-2 ubuntu 发行版的 vscode 上编程。 我认为我确实正确设置了系统,但我在代码中包含的库中的标识符上不断出现红色波浪线。 代码如下所示:

#include <iostream>
#include <stddef.h>
#include <cstring>
#include "string.h"

String::String () {
    this->data=NULL; 
    this->length=0; 
}

String::String (const String &str) {
    if (str.data==NULL || str.length==0) {
        this->data=NULL;
        this->length=0;
    } else {
        this->length = str.length;
        this->data = new char [str.length+1];
        strncpy(this->data, str.data, str.length+1);
    }
}

String::String (const char *str) {
    if (str==NULL) {
        this->data=NULL;
        this->length=0;
    } else {
        this->length = strlen(str);
        this->data = new char [strlen(str)+1];
        strncpy(this->data, str, this->length+1);
    }
}

我在 NULL、strncpy、strlen 等方面遇到错误。

这是 cpp json 文件:

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/linux"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c11",
            "cppStandard": "c++20",
            "intelliSenseMode": "linux-gcc-x64"
        }
    ],
    "version": 4
}

我试着在网上找遍了,看起来像这样没问题。而且我确实相信我安装了所有先决条件。 Screenshot of the code errors

有人知道为什么会发生这种情况吗?

我不会有一个名为“string.h”的 header;因为 NULL 的 C-header,strncpy 等也在“string.h”中。

可能 #include <cstring> 包括 你的 string.h 而不是系统 string.h,导致你的错误。

请注意 cstring-header 通常包含如下代码:

...
#include <string.h>
...

您也可以尝试从包含路径中删除本地目录,因为 #include <string.h> 开始在包含路径中搜索 - 而 #include "string.h" 也应该找到本地文件。