views:

101

answers:

5

Hello, 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.

+10  A: 

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).
Greg S
See [this answer](http://stackoverflow.com/questions/2879555/c-stl-how-to-write-wrappers-for-cout-cerr-cin-and-endl/2880136#2880136) for why I don't think `using namespace std` is a good idea.
sbi
@sbi: you're absolutely right, edited to reflect that `using` in a header is bad practice.
Greg S
@Greg: I have now up-voted your answer. `:)`
sbi
I only use `using std::something;` in a cpp file, safest thing to do. I leave the prefixes in all headers.
rubenvb
Oh, and in the topic of `using namespace std;`. I am using QT combined with standart C++ (I actually prefer standart `fstream`, maybe I will get used to QT`s stuff later). So, when I include `fstream`, on compiliation I get an error that it says:`expected unqualified-id before 'namespace'` on iostream`s on 43`rd line (if I include both `iostream` and `fstream`) or the same error on codecvt.h 42`nd line (if I only include `fstream`). So am I forced to use QT`s stuff, or I still ould get fstream working?
EdgeLuxe
@EdgeLuxe: Well, iostreams certainly work for us, so you must be doing something wrong. You might want to boil this down to 20 lines and post it as a new question so that we can look at it.
sbi
+9  A: 

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);
Timbo
+3  A: 

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

Use std::string

Bertrand Marron
+3  A: 

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
Sjoerd
A: 

put

using namespace std

under

#include <string>
MBZ
Almost 15mins before you posted your answer, I commented to [Greg's answer](http://stackoverflow.com/questions/3307866/c-declaring-a-class-with-functions-that-handle-string/3307877#3307877) why I think that's a bad idea.
sbi
-1 It is a bad habit to have namespace-level `using` directives in header files. Additionally your code is broken...(you are missing a semicolon).
smerlin