C++:声明一个带有函数的类,它处理字符串

1 c++ class declaration definition

我没有使用搜索找到我的问题的答案,但我认为这很简单而且很受欢迎.无论如何,我的问题是:我有一个头文件,它声明了一个类和函数.它看起来像这样:

#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
Run Code Online (Sandbox Code Playgroud)

我认为功能定义现在并不重要.

在编译时,编译器告诉'string'尚未声明,即使我已经添加了#include <string>.除了重写要使用的函数之外,我该如何解决这个问题char*.先感谢您.

完成.谢谢大家.

Gre*_*g S 10

问题:string类位于命名空间中std.

解决方案:

  1. 最佳解决方案:只需使用std::string而不是 string在函数声明中.
  2. 另一个不太理想的解决方案:using namespace std;在include指令后添加(有关缺点/危险的解释using,请参阅sbi注释中的链接).

  • 请参阅[这个答案](http://stackoverflow.com/questions/2879555/c-stl-how-to-write-wrappers-for-cout-cerr-cin-and-endl/2880136#2880136)为什么我不喜欢我认为`使用namespace std`是一个好主意. (2认同)

Tim*_*mbo 8

string在命名空间中声明std,因此您必须将函数声明更改为

bool a_func(std::string & myString, unsigned long int & x);
Run Code Online (Sandbox Code Playgroud)