How come when I do
using std::string;
I get an error but after I include string, it doesn't throw error. Same with std::setprecision.
#include <string>
using std::string; //Throws error when string library not included
#include <iomanip>
using std::setprecsion; //Throw error when iomanip library not included
int main()
{
//stuff
}
When I leave out the #include preprocessor, why does it throw an error saying string or setprecision is not part of std namespace, but when I add the libraries they don't? Do they somehow get added to std namespace after including the libraries?
Would it kind of be like bracket notation in JavaScript?
var obj = {
one: 1
};
obj['two'] = 2;
It is simply because the functions you are trying to use are defined in their library files. In C , C++ you have to write #include to include the library in your program and use its functions.
Related
#include <string.h>
using namespace std;
namespace charcount
{
int ShowPerCent();
int PerCent();
int Values(char letter);
int analize(string var);
}
This code is all part of "functions.h" of my project. This says:
functions.h: 7:13: error: 'string' was not declared in this scope
And I don't understand why says that this. I try with std::string and nope. Anyone know what happens? If you need more additional information ask.
The correct header is <string>. Change the include directive to:
#include <string>
The C++ standard library headers do not end with .h.
It's considered very bad practice to do using namespace std;, especially in a header file. This pollutes the global namespace with names from the std namespace and propagates said pollution to any file that includes it.
In C,
#include <string.h>
gives you the C string header (strlen(), strcmp() et al.).
In C++,
#include <string.h>
is deprecated, but gives you the same C string header. You are encouraged to use
#include <cstring>
instead, which gives you the same functions but in the std:: namespace (where they belong).
If you want std::string, the object-oriented auto-allocating auto-expanding C++ niceness, you would have to:
#include <string>
And please, don't use using namespace, especially not in combination with std::. The idea is to be explicit about which namespace a given identifier comes from.
Edit: Seconding sftrabbit, who typed quicker than me. While using namespace might be pardonable in your .cpp files, in headers it's a capital offense, because including your header could make perfectly valid C++ code invalid all of a sudden, because you changed the namespace.
I can't declare a string in my program:
string MessageBoxText = CharNameTextBox->Text;
it just doesn't work. It says string is undeclared identifier. What am I missing in the namespace or include or something like that?
Make sure you've included this header:
#include <string>
And then use std::string instead of string. It is because string is defined in std namespace.
And don't write this at namespace scope:
using namespace std; //bad practice if you write this at namespace scope
However, writing it at function scope is not that bad. But the best is one which I suggested before:
Use std::string as:
std::string MessageBoxText = CharNameTextBox->Text;
To use the standard string class in C++ you need to #include <string>. Once you've added the #include directive string will be defined in the std namespace and you can refer to it as std::string.
E.g.
#include <string>
#include <iostream>
int main()
{
std::string hw( "Hello, world!\n" );
std::cout << hw;
return 0;
}
Are you by any way compiling using C++/CLI, the Microsoft extension for .NET, and not standard ISO C++?
In that case you should do the following:
System::String^ MessageBoxText = CharNameTextBox->Text;
Also see the following articles:
How to: Convert Between Various String Types
How to: Convert System::String to Standard String
How to: Convert Standard String to System::String
I am receiving the error: identifier "string" undefined.
However, I am including string.h and in my main file, everything is working fine.
CODE:
#pragma once
#include <iostream>
#include <time.h>
#include <string.h>
class difficulty
{
private:
int lives;
string level;
public:
difficulty(void);
~difficulty(void);
void setLives(int newLives);
int getLives();
void setLevel(string newLevel);
string getLevel();
};
Can someone please explain to me why this is occurring?
<string.h> is the old C header. C++ provides <string>, and then it should be referred to as std::string.
You want to do #include <string> instead of string.h and then the type string lives in the std namespace, so you will need to use std::string to refer to it.
You forgot the namespace you're referring to. Add
using namespace std;
to avoid std::string all the time.
Because string is defined in the namespace std. Replace string with std::string, or add
using std::string;
below your include lines.
It probably works in main.cpp because some other header has this using line in it (or something similar).
Perhaps you wanted to #include<string>, not <string.h>. std::string also needs a namespace qualification, or an explicit using directive.
You must use std namespace. If this code in main.cpp you should write
using namespace std;
If this declaration is in header, then you shouldn't include namespace and just write
std::string level;
#include <string> would be the correct c++ include, also you need to specify the namespace with std::string or more generally with using namespace std;
I am new to c++, trying to debug the following line of code
class cGameError
{
string m_errorText;
public:
cGameError( char *errorText )
{
DP1("***\n*** [ERROR] cGameError thrown! text: [%s]\n***\n",
errorText );
m_errorText = string( errorText );
}
const char *GetText()
{
return m_errorText.c_str();
}
};
enum eResult
{
resAllGood = 0, // function passed with flying colors
resFalse = 1, // function worked and returns 'false'
resFailed = –1, // function failed miserably
resNotImpl = –2, // function has not been implemented
resForceDWord = 0x7FFFFFFF
};
This header file is included in the program as followed
#include "string.h"
#include "stdafx.h"
#include "Chapter 01 MyVersion.h"
#include "cGameError.h"
You need to include <string>, not "string.h". Or in addition to "string.h".
string.h is the C header for the standard C string handling functions (strcpy() and friends.)
<string> is the standard C++ header where 'string' is defined.
You also need to specify the std namespace when using string:
std::string m_errorText;
Or by using:
using namespace std;
Somewhere at the top of your file.
You should also use angle brackets for system include files.
You've provided little enough information that this is only a wild guess, but at first glance, I'd guess the problem is that you haven't included <string>, only "string.h" (the former defines the C++ std::string class, the latter the C functions for manipulating nul-terminated strings.
As an aside, you normally want to use angle-brackets for system headers, so it should be <string.h>.
Try #include <string>, instead of #include "string.h", string.h/cstring is the old C-string header, string is the new C++ std::string class header. And you normally use angle-brackets for system headers.
I know this is quite a ridiculous question but this is quite confusing and irritating, as something that should work simply is not. I'm using Code Blocks with the GCC compiler and I am trying to simply create a string variable in my class
#ifndef ALIEN_LANGUAGE
#define ALIEN_LANGUAGE
#include <string>
class Language
{
public:
private:
string str;
};
#endif
Strange enough, my compiler halts me with an error saying this:
C:\Documents and Settings\...|11|error: `string' does not name a type|
||=== Build finished: 1 errors, 0 warnings ===|
For some reason, it is unable to find the class "string" which for some reason, my main.cpp is able to detect "#include " while my language class is not able for some reason.
This is the main I wrote quickly just to see it main itself is able to see the string file:
//main.cpp
#include <iostream>
#include <string>
#include "alien_language.h"
using namespace std;
int main()
{
string str;
return 0;
}
Does anyone know what's going on?
using namespace std;
That's what's going on.
You don't have std:: prefixing the string in your class. Everything in the standard library is in the namespace std.
It is generally regarded as bad practice to use using namespace std;, by the way. For more information on why and what to do instead, check out this question: Using std Namespace.
The string class is defined in the std namespace. You should chenge the class to this:
class Language
{
public:
private:
std::string str;
};
It is also possible, but not recommended to add this to the top of the header file:
using namespace std;
string is in namespace std, and you need to qualify it fully inside your header file:
#include <string>
class Language
{
public:
private:
std::string str;
};
Do not use using namespace std; or similar in header files.
You should refer to it as std::string;
It looks to me like you're missing the all-important (with a hint of sarcasm) using namespace std; line. Either add that in before your class, or explicitely use std::string str. I'd recommend against adding the using namespace std; line in a header file, as it would pollute the mainspace for any file that includes it.
The string class in standard C++ is in std namespace. Write something like
using std::string; in your header or fully qualify it as std::string in your header.
Beware that using namespace std; in header is a bad practice (read here).