Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ "std::string has not been declared" error

Tags:

c++

string

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.

like image 827
wannabe programmer Avatar asked Jun 11 '13 08:06

wannabe programmer


People also ask

Can you use std::string in C?

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.

How do I fix the string was not declared in this scope?

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.

Why is there no string in C++?

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.

Where is std::string defined?

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.


2 Answers

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.

like image 200
Bathsheba Avatar answered Sep 20 '22 09:09

Bathsheba


The include should come before the using

#include <string>
using namespace std;
//using std::string; <-- Needless
like image 33
Roee Gavirel Avatar answered Sep 21 '22 09:09

Roee Gavirel