0

I have had this problem for a while where my Linux system can't seem to link any header files(and yes, I have checked: they all exist in the right locations). Here is a basic script called HelloWorld.cpp:

#include <iostream>
#include </usr/include/string.h>

int main(){
string hi = "Hello WOrld!";
return 0;
}

And this is the error output from the terminal:

HelloWorld.cpp: In function int main():
HelloWorld.cpp:5:1: error: string was not declared in this scope
 string hi = "Hello WOrld!";
 ^~~~~~
HelloWorld.cpp:5:1: note: suggested alternative:
In file included from /usr/include/c++/8/iosfwd:39,
                 from /usr/include/c++/8/ios:38,
                 from /usr/include/c++/8/ostream:38,
                 from /usr/include/c++/8/iostream:39,
                 from HelloWorld.cpp:1:
/usr/include/c++/8/bits/stringfwd.h:74:33: note:   ‘std::__cxx11::string’
    typedef basic_string<char>    string;
                                 ^~~~~~

What can I do?

  • 1
    I would strongly recommend getting a good C++ book: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – jjramsey Jul 30 '21 at 17:28
  • 4
    `string.h` is for C-style string routines like `strlen`. You want to include `` (no .h), and use `std::string hi = "Hello World!";` – jkb Jul 30 '21 at 17:29
  • 2
    Nit-picking: Headers are _included_ (when C++ files a _compiled_). Object files and libraries are _linked_. Please, get used to this or expect more complaints (if you talk to C++ developers). ;-) – Scheff's Cat Jul 30 '21 at 17:34

2 Answers2

0

You need #include <string>

ShahzadIftikhar
  • 515
  • 2
  • 11
  • 3
    While this is correct in general it seems to me that the compiler found `std::string` (probably indirectly included via ``) but the type `string` (in global scope) wasn't matching as the OP (surprisingly) didn't use the (ugly) `using namespace std;`. So, `#include ` is only the half of the story... – Scheff's Cat Jul 30 '21 at 17:37
0

This seems like the best answer posted so far:

string.h is for C-style string routines like strlen. You want to include (no .h), and use std::string hi = "Hello World!"; -jkb

Thank you for answering my question.