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.

3
  • 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. Commented Jul 22, 2010 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. Commented Jul 22, 2010 at 10:49
  • @Pontus and @DanDan are absolutely right. See this answer for a set of rules of thumb for to passing function arguments in C++. Commented Jul 22, 2010 at 10:56

5 Answers 5

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).
Sign up to request clarification or add additional context in comments.

5 Comments

See this answer for why I don't think using namespace std is a good idea.
@sbi: you're absolutely right, edited to reflect that using in a header is bad practice.
I only use using std::something; in a cpp file, safest thing to do. I leave the prefixes in all headers.
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 QTs stuff later). So, when I include fstream, on compiliation I get an error that it says: expected unqualified-id before 'namespace'` on iostreams on 43rd line (if I include both iostream and fstream) or the same error on codecvt.h 42nd line (if I only include fstream). So am I forced to use QTs stuff, or I still ould get fstream working?
@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.
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);

Comments

3

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

Use std::string

Comments

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

Comments

-2

put

using namespace std

under

#include <string>

2 Comments

Almost 15mins before you posted your answer, I commented to Greg's answer why I think that's a bad idea.
-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).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.