【问题标题】:<string> header works but not <cstring> [closed]<string> 标头有效,但 <cstring> [关闭]
【发布时间】:2014-08-16 21:23:46
【问题描述】:

我从我的项目中改编了三个短文件,因为它们重现了我遇到的错误:

exp.cpp

#include <iostream>
using namespace std;

int main(){
    cout << "Hello world!" << endl;
}

解析器.cpp

#include <cstring>
#include "parser.h"

解析器.h

#ifndef PARSER_H_
#define PARSER_H_

#include <cstring>

class Parser{
public:
    Parser(std::string*);
    std::string* unprocessedInput;
};

#endif /* PARSER_H_ */

当我构建项目时,在行,

Parser(std::string*)

我在“*”标记之前收到错误消息“预期的‘)”,并且在该行,

std::string* unprocessedInput;,

我收到错误“命名空间‘std’中的‘字符串’没有命名类型”。

当我在 parser.cpp 或 parser.h 中使用 而不是 时,项目将构建。我想了解为什么 不起作用。我需要在我的项目中使用以 null 结尾的字符串。

请注意,我已经对此进行了广泛的搜索。在我的研究中,我想到了在头文件中使用 std::string 而不是字符串。

【问题讨论】:

  • 您知道问题所在,那么您的问题有什么意义呢?只需包含您正在执行的操作的正确标题即可。
  • 我想使用以空字符结尾的字符串,这就是我想使用 的原因。如果我使用 是否意味着我需要将每个字符串都转换为 cstring?

标签: c++ string cstring


【解决方案1】:

标头 &lt;cstring&gt; 包含 C 标头 &lt;string.h&gt; 的标准 C 声明 C++ 头文件&lt;string&gt; 包含C++ 标准类std::string (std::basic_string) 的声明

所以&lt;cstring&gt;&lt;string&gt; 是不同的标头并包含不同的声明。

根据 C++ 标准(相对于标头 &lt;cstring&gt;

7 内容与标准C库头文件相同 &lt;string.h&gt;,在 21.8 中指定了对 memchr() 的更改。

【讨论】:

    猜你喜欢
    • 2020-06-11
    • 2011-09-01
    • 2014-09-12
    • 1970-01-01
    • 2012-12-24
    • 2012-10-01
    • 2018-02-26
    • 2014-08-13
    • 1970-01-01
    相关资源
    最近更新 更多