I've been looking for an answer on websites but couldn't find any answer that helped me.
I have a code that uses strings when i tried (like suggested) to add these lines:
using namespace std;
using std::string;
#include <string>
I tried to use each of them seperately and I tried all of them together. The best situation was when all of the string errors disappeared but I had another weird error on the line "using std::string" and the error was: std::string has not been declared. Any ideas? Thanks guys.
A std::string_view can refer to both a C++ string or a C-string. All that std::string_view needs to store is a pointer to the character sequence and a length. std::string_view provides the same API that std::string does, so it is a perfect match for C-style string literals.
You have the following options: Write using namespace std; after the include and enable all the std names: then you can write only string on your program. Write using std::string after the include to enable std::string : then you can write only string on your program. Use std::string instead of string.
So as per standard OOP norms, string in C++ can't act as a class whose objects store values themselves and can access its methods.
std::string is a typedef for a particular instantiation of the std::basic_string template class. Its definition is found in the <string> header: using string = std::basic_string<char>; Thus string provides basic_string functionality for strings having elements of type char.
Put #include <string>
first.
Avoid using
statements in headers as you potentially bring in all sorts of stuff into many compilation units. using std::string
is perhaps acceptable in a header but using namespace std
certainly isn't as it will cause so much namespace pollution in all compilation units. The std namespace is continuously expanding (look at all the new stuff in C++), so you don't want to have to fix lots of errors when upgrading your compiler.
The include
should come before the using
#include <string>
using namespace std;
//using std::string; <-- Needless
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With