Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<string> in header file

Tags:

c++

string

header

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.

like image 692
Bogdan Maier Avatar asked Apr 30 '12 17:04

Bogdan Maier


People also ask

What is a string header file?

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.

Should I include string in header file C++?

h> Inclusion of <string> In C++ is recommended when the program needs to use string. The same purpose is there for inclusion of <string.

What is string H header?

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.

What is #include string for?

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.


1 Answers

string lives in namespace std. Make that:

#include <string>

class Foobar {
    int a;
    std::string foo;
};
like image 189
ildjarn Avatar answered Oct 04 '22 22:10

ildjarn