user380041
user380041

Reputation:

C++: declaring a class with functions, that handle string

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

Answers (5)

MBZ
MBZ

Reputation: 27592

put

using namespace std

under

#include <string>

Upvotes: -2

Greg S
Greg S

Reputation: 12538

Problem: the string class resides in namespace std.

Solutions:

  1. Best solution: simply use std::string instead of string in your function declarations.
  2. Another, less optimal solution: add 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

Sjoerd
Sjoerd

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

Bertrand Marron
Bertrand Marron

Reputation: 22210

The type string that you're willing to use is declared in a namespace called std.

Use std::string

Upvotes: 3

Timbo
Timbo

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

Related Questions