1

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.

  • 5
    Unless the functions really modify the strings, you should declare the parameters as `const std::string &`. That allows you to call them with string literals. – Pontus Gagge Jul 22 '10 at 10:46
  • 4
    There's also no need to pass native types, like ints, as reference unless you plan to modify them in the function. – DanDan Jul 22 '10 at 10:49
  • @Pontus and @DanDan are absolutely right. See [this answer](http://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c/2139254#2139254) for a set of rules of thumb for to passing function arguments in C++. – sbi Jul 22 '10 at 10:56

5 Answers5

10

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
  • 12,333
  • 2
  • 41
  • 48
  • 2
    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 Jul 22 '10 at 10:36
  • @sbi: you're absolutely right, edited to reflect that `using` in a header is bad practice. – Greg S Jul 22 '10 at 10:39
  • I only use `using std::something;` in a cpp file, safest thing to do. I leave the prefixes in all headers. – rubenvb Jul 22 '10 at 10:54
  • 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? –  Jul 22 '10 at 11:23
  • @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 Jul 24 '10 at 06:06
8

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
  • 27,472
  • 11
  • 50
  • 75
3

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

Use std::string

Bertrand Marron
  • 21,501
  • 8
  • 58
  • 94
3

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
  • 6,837
  • 31
  • 44
-2

put

using namespace std

under

#include <string>
MBZ
  • 26,084
  • 47
  • 114
  • 191
  • 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 Jul 22 '10 at 10:54
  • -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 Jul 22 '10 at 11:23