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

发布于 2024-09-10 14:43:02 字数 677 浏览 9 评论 0原文

我还没有使用搜索找到我的问题的答案,尽管我认为它简单且流行。无论如何,我的问题是: 我有一个头文件,其中声明了一个类和函数。看起来是这样的:

#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 。除了重写函数以使用 char* 代替之外,我该如何解决这个问题。先感谢您。

完毕。谢谢大家。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

已下线请稍等 2024-09-17 14:43:02

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

解决方案:

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

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).
如梦亦如幻 2024-09-17 14:43:02

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

bool a_func(std::string & myString, unsigned long int & x);

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);
跨年 2024-09-17 14:43:02

您愿意使用的类型 string 是在名为 std 的命名空间中声明的。

使用 std::string

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

Use std::string

云之铃。 2024-09-17 14:43:02

中定义的类型称为 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

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
看轻我的陪伴 2024-09-17 14:43:02

放在

using namespace std

下面

#include <string>

put

using namespace std

under

#include <string>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文