0

I am trying to program on vscode on wsl-2 ubuntu distro. I think I did setup the system correctly, but I keep getting red squiggles on identifiers that are in libraries I included in the code. The code looks like this:

#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);
    }
}

I get errors on NULL, strncpy, strlen and such.

This is the cpp json file:

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

I tried to look all over the internet, no problem looks like this. And I do believe I installed all prerequisites. Screenshot of the code errors

Anyone has an idea why this can happen?

Etxx321
  • 3
  • 2
  • 2
    `NULL` -> `nullptr`. `NULL` was deprecated in C++11. You're not including the correct headers for the other functions. Don't guess them or rely on implicit inclusions. – Bathsheba May 24 '21 at 13:03
  • You may want to read this: [What exactly is nullptr?](https://stackoverflow.com/q/1282295/12149471) – Andreas Wenzel May 24 '21 at 13:06
  • What about strcpy? and other functions that are declared in my includes? – Etxx321 May 24 '21 at 13:08
  • You may want to read this: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/12149471) – Andreas Wenzel May 24 '21 at 13:09
  • 1
    `std::strcpy` is in the header ``. – Eljay May 24 '21 at 13:09
  • Does it work if you change `strncpy` to `std::strncpy`? – Andreas Wenzel May 24 '21 at 13:10
  • Adding std:: doesn't work – Etxx321 May 24 '21 at 13:18
  • Why are you using the function `strncpy` at all? That function was never intended for use with modern null-terminated strings, but rather with fixed-length strings that may be prematurely null-terminated. Such strings were common in the 1980s (40 years ago), but are no longer in use today. If you are under the impression that `strncpy` is a more secure version of `strcpy`, then you are mistaken, as that function was never designed as such. – Andreas Wenzel May 26 '21 at 19:52

1 Answers1

1

I wouldn't have a header named "string.h"; since the C-header for NULL, strncpy etc is also in "string.h".

It might be that #include <cstring> includes your string.h instead of the system string.h, leading to your error.

Note that the cstring-header normally contain code like:

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

You might also try to remove the local directory from your include path, since #include <string.h> starts searching in the include path - whereas #include "string.h" should also find local files.

Hans Olsson
  • 11,123
  • 15
  • 38