0

I am very new to coding in C++ and have little to no experience at all. My problem is: I wanted to use strings so i added #include to the includes in my code but VSCode tells me identifier "string" is undefinedC/C++(20). What I have done is added and modified a c_cpp_properties.json file vor VSCode:

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

But that didn't solve the problem. Also, I have made sure that C++ is installed, including the g++ compiler. In VSCode I am using following C++ plugins / addons: C/C++ and C/C++ Project Generator (which I don't really use). This is the relevant part of my code:

#include <iostream>
#include <string>

int main(int argc, char *argv[])
{
string s;

return 0;
}

And this is the error log / debug message:

g++ -std=c++17 -Wall -Wextra -g -Iinc -c src/main.cpp  -o src/main.o
src/main.cpp: In function ‘int main(int, char**)’:
src/main.cpp:30:2: error: ‘string’ was not declared in this scope
   30 |  string s;
      |  ^~~~~~
src/main.cpp:30:2: note: suggested alternatives:
In file included from /usr/include/c++/9/iosfwd:39,
                 from /usr/include/c++/9/ios:38,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from src/main.cpp:1:
/usr/include/c++/9/bits/stringfwd.h:79:33: note:   ‘std::string’
   79 |   typedef basic_string<char>    string;
      |                                 ^~~~~~
In file included from /usr/include/c++/9/bits/locale_classes.h:40,
                 from /usr/include/c++/9/bits/ios_base.h:41,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from src/main.cpp:1:
/usr/include/c++/9/string:67:11: note:   ‘std::pmr::string’
   67 |     using string    = basic_string<char>;
      |           ^~~~~~

Solution: Replace string s; with std::string s;.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 1
    Is this just a typo ? `string` should be `std::string` – Richard Critten May 16 '22 at 19:31
  • Using a Microsoft tool on a linux system? Strange setup... Never been convinced of that tool – has it been the first tool to cross your route or was it a witting decision? Might recommend some alternatives in former case. – Aconcagua May 16 '22 at 19:32
  • `identifier "string" is undefinedC/C++(20)` – that error message doesn't hint to a bad header – shouldn't there be a line number coming with? If it really was the header the line number should correspond to the line of the `#include`, instead I'd expect it, though, to point to the line with your variable declaration `string s` – see Richard's comment! – Aconcagua May 16 '22 at 19:36
  • Oh, and before you might discover and and think it's nice: Please don't even start with [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua May 16 '22 at 19:37
  • Well, VSCode is owned by Microsoft but all code is open-source and under the GPL license... don't confuse it with Visual Studio (without the code) which is a fully blown IDE (I suppose). VSCode is just somewhat of a primitive text editor that can run terminal / console / shell commands. (And has a lot of plugins that help with coding.) – iJustLeyxo May 16 '22 at 19:40
  • Pretty well aware of – doesn't change my *personal* judgement. But it's *your* choice, if you are happy with feel free to stay with ;) – Aconcagua May 16 '22 at 19:48
  • Personal judgment is a different beast than what your first comment implies, which is unfamiliarity. – sweenish May 16 '22 at 19:55

1 Answers1

-1

C++ strings are "hidden" inside the std namespace. The compiler doesn't recognize string because of that.

You could either include the entire std namespace:

using namespace std;

Do this for specifically strings and nothing else:

using std::string;

or type std::string instead of string when initializing strings. The C++ Standard Library is in the compiler's include path by default, you don't need to do anything extra to be able to use its headers.

shell_
  • 29
  • 3
  • No, please don't recommend [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – it's source of just so many questions here on SO when function names clash... `using std::xyz` is acceptable in source files, though I'd still rather only use it at local scope (i.e. within functions). Avoid it at global scope in header files, as you might spoil other people's global namespace if they are using them. – Aconcagua May 16 '22 at 19:39