I`m trying to define a variable of string type in a class definition in a header file. Is it possible? Example:
/* Foo.h */
#include <string>
class Foobar{
int a;
string foo;
}
Because somehow in main I can declare a string variable, but in the header it doesn’t recognize my string type.
h is the header file required for string functions. This function appends not more than n characters from the string pointed to by src to the end of the string pointed to by dest plus a terminating Null-character.
h> Inclusion of <string> In C++ is recommended when the program needs to use string. The same purpose is there for inclusion of <string.
h is the header in the C standard library for the C programming language which contains macro definitions, constants and declarations of functions and types used not only for string handling but also various memory handling functions; the name is thus something of a misnomer. Functions declared in string.
Use #include <string> when you use a variable that has type std::string . The code "text here" , contrary to intuition, is not a std::string ; it is a string literal, and a C-style string, and a const char[10] convertible to const char* . Welcome to C++ with its legacy oddities.
string
lives in namespace std
. Make that:
#include <string>
class Foobar {
int a;
std::string foo;
};
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