Dear All,
Although I put preprocessor command
#include<string>
my variables are not recognized as of string type and I get error c2065. What is the problem?
Thank you
Dear All,
Although I put preprocessor command
#include<string>
my variables are not recognized as of string type and I get error c2065. What is the problem?
Thank you
std::string
or
using namespace std;
You probably need to use "std::string", "using std::string" or "using namespace std", as modern C++ has "namespaces" , and the string implementation is in the "std" namespace.
Namespaces avoid conflicts between different names in different portions of the code, and avoid having to put "ModuleX" in front SomeFunction() to make it unique to ModuleX. This is important for large projects that have many different source files, and for example "FindCustomer()" or "OpenInputFile" may be function names that makes sense to many different parts of the code - but you don't want the invoice module calling the "FindCustomer" of the late payment penalty module, for example.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Thank you for the answer!!
I have got a question: if I have many source and hearder files in the project, in which I use type string or any other type - Do I have to put line
using namespace std;
in each of these header or source files or there is a way to implement std namespace (or any other) once for the whole project?
Thank you!!!
Use in every source file is safest. You can also use it in a header and include it in every source file (but this isn't recommended).
You must have it on a .cpp file basis.
The reason you don't want to put it in a header file is that it's then not obvious where your "string" comes from. It may well be that someone implements a "better" improved::string version, that wholly replaces the std::string. Now, if you use an include file to include "using namespace std", then you don't know by looking at a file if it's using std::string or improved::string, do you?
That's of course just one of many examples.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
You need to have say
using namespace std;
or better
using std::string
or best of all
std::string myvar;
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.