3

我在尝试为自己键入一个方便的 tstring 时遇到问题(见下文)

#ifndef _NISAMPLECLIENT_H_
#define _NISAMPLECLIENT_H_

#include <windows.h>
#include <stdlib.h>
using namespace std; // ERROR here (1)

#ifdef _UNICODE
#define CommandLineToArgv CommandLineToArgvW
#else
#define CommandLineToArgv CommandLineToArgvA
#endif

typedef basic_string<TCHAR> tstring; // ERROR HERE (2)

尝试编译时出现编译器错误。“ERROR here (1)”处的错误是:

错误 3 错误 C2871:“std”:具有此名称的命名空间不存在 \nisampleclient\nisampleclientdefs.h 16

如果我删除using namespace std;声明并将 ERROR HERE (2) 更改为说,typedef std::basic_string<TCHAR> tstring;那么我会收到一个错误:

错误 3 错误 C2653: 'std' : 不是类或命名空间名称 \nisampleclient\nisampleclientdefs.h 23

那时相反。

提前致谢。:)

4

2 回答 2

7

包括string标题(#include <string>,而不是 string.h ;))。

另外,永远不要使用:

using namespace ...

...在标题中,除非您想激怒其他开发人员;)

旁注:在 C++ 中,大多数传统 C 标准头文件都有对应部分,没有.h扩展名,但前导c. 在您的情况下#include <cstdlib>将是更好的选择,尽管这取决于您使用的编译器是否存在实际差异。

于 2011-02-24T15:14:22.783 回答
5

std::basic_string类模板接受三个参数。所以你必须这样做:

 #include <string> //include this

 typedef std::basic_string<TCHAR, std::char_traits<TCHAR>, std::allocator<TCHAR> > tstring;
于 2011-02-24T15:14:40.293 回答