为什么在包含 <string.h> 并使用 std::basic_string 时会出现编译器错误?

问题描述 投票:0回答:1

当我尝试编译以下代码时:

#include <string.h>
using namespace std;
typedef std::basic_string<char> foostring;
foostring foo = "foo";

我收到以下错误:

stringtest.cpp:5: error: expected initializer before ‘<’ token
stringtest.cpp:6: error: ‘foostring’ does not name a type

我的编译器是:g++ (Ubuntu 4.4.1-4ubuntu9) 4.4.1
我究竟做错了什么?一旦我弄清楚如何使用它,我打算将其与 Windows TCHAR 一起使用以支持 unicode。

c++ stdstring
1个回答
4
投票

标题是

<string>
,而不是
<string.h>

所有标准库头文件都不以扩展名结尾。 (您正在包含 C 头文件

string.h
,如果您真正想要的话,它应该通过
<cstring>
包含在 C++ 中。)

© www.soinside.com 2019 - 2024. All rights reserved.