Reputation:
I haven`t found answer to my question using search, though I thought it is simple and popular. Anyway, my question is: I have got a header file, which declares a class and functions in it. It looks like that:
#ifndef SOME_CLASS_H
#define SOME_CLASS_H
#include <string>
class mySomeClass
{
public:
bool a_func(string & myString, unsigned long int & x);
void b_func(string & myString, unsigned long int & x);
void c_func(string & myString, unsigned long int & x);
void another_func(string & myString, string & myString2);
}
#endif // SOME_CLASS_H
I think function definitions do not actually matter now.
When compiling, compiler tells that 'string' has not been declared, even though I have added #include <string>
. How can I solve this except for rewriting functions to use char*
instead. Thank you in advance.
Done. Thanks everybody.
Upvotes: 1
Views: 1870
Reputation: 12538
Problem: the string
class resides in namespace std
.
Solutions:
std::string
instead of
string
in your function declarations.using namespace std;
after the
include directive (for an explanation of the drawbacks/dangers of using
, see the link in sbi's comment).Upvotes: 10
Reputation: 6875
The type defined in <string> is called std::string, not just string.
#ifndef SOME_CLASS_H
#define SOME_CLASS_H
#include <string>
class mySomeClass
{
public:
bool a_func(std::string & myString, unsigned long int & x);
void b_func(std::string & myString, unsigned long int & x);
void c_func(std::string & myString, unsigned long int & x);
void another_func(std::string & myString, std::string & myString2);
}
#endif // SOME_CLASS_H
Upvotes: 3
Reputation: 22210
The type string
that you're willing to use is declared in a namespace called std
.
Use std::string
Upvotes: 3
Reputation: 28050
string
is declared in the namespace std
, so you have to change the function declarations to
bool a_func(std::string & myString, unsigned long int & x);
Upvotes: 8