我没有使用搜索找到我的问题的答案,但我认为这很简单而且很受欢迎。无论如何,我的问题是: 我有一个头文件,它声明了一个类和函数。它看起来像是:
#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
我认为功能定义现在并不重要。
编译时,编译器告诉'string'尚未声明,即使我添加了#include <string>
。除了重写函数以使用char*
之外,我该如何解决这个问题。提前谢谢。
完成。谢谢大家。
答案 0 :(得分:10)
问题:string
类位于名称空间std
。
解决方案:
std::string
代替
函数声明中的string
。using namespace std;
include指令(有关using
的缺点/危险的解释,请参阅sbi评论中的链接)。答案 1 :(得分:8)
string
在名称空间std
中声明,因此您必须将函数声明更改为
bool a_func(std::string & myString, unsigned long int & x);
答案 2 :(得分:3)
您愿意使用的类型string
在名为std
的名称空间中声明。
使用std::string
答案 3 :(得分:3)
&lt; string&gt;中定义的类型被称为std :: 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
答案 4 :(得分:-2)
把
using namespace std
下
#include <string>