C++错误:“串”没有指定类型

问题描述:

当我编译下面的文件,我已经得到了错误:C++错误:“串”没有指定类型

ECArgs.h:36:3: error: ‘string’ does not name a type 

ECArgs.h:36:ECString值(字符C);

有人能给我任何错误提示吗?

ECArgs.h

#include <list> 
#include "ECString.h" 

class ECArgs 
{ 
public: 
    ECArgs(int argc, char *argv[]); 
    int nargs() { return nargs_; } 
    bool isset(char c); 
    ECString value(char c); 
    ECString arg(int n) { return argList[n]; } 
private: 
    int nargs_; 
    int nopts_; 
    ECString argList[32]; 
    list<ECString> optList; 
}; 

ECString.h

#define ECS gnu 

#if ECS == gnu 
#include <cstring> 
#define ECString string 
using namespace std; 
#else 
#include <bstring.h> 
#define ECString string 
#endif 
+0

这不是“有线”错误。不从非包含的头中识别符号是完全合理的。此外,'typedef'和'using'是比'#define'更好的类型别名解决方案。 – chris 2013-03-04 04:08:47

+0

@chris你的意思是不包含头文件? – 2013-03-04 04:11:11

+0

如果你想访问'std :: string',你需要'#include ',而不是'#include '。 – jogojapan 2013-03-04 04:13:05

您需要添加:

#include <string> 

cstring包括功能来操纵C风格的字符串。此版本的作品:

#include <list> 
#include <string> 

#if ECS == gnu 
#include <cstring> 
#define ECString string 
using namespace std; 
#else 
#include <bstring.h> 
#define ECString string 
#endif 

class ECArgs 
{ 
public: 
    ECArgs(int argc, char *argv[]); 
    int nargs() { return nargs_; } 
    bool isset(char c); 
    ECString value(char c); 
    ECString arg(int n) { return argList[n]; } 
private: 
    int nargs_; 
    int nopts_; 
    ECString argList[32]; 
    list<ECString> optList; 
}; 

int main() 
{ 

} 
+0

只是想知道为什么cstring不起作用。我试图在另一个test.cpp文件中使用'string',其中包含。 test.cpp运行良好。 – 2013-03-04 04:14:44

+0

@EarthWorm你可能包含另一个包含字符串的头文件吗?我正在看gcc 4.4.4和几个头包括字符串,包括随机和stdexcept。 – 2013-03-04 04:23:24

+0

你说得对。我包括。非常感谢 – 2013-03-04 04:33:54

我遇到了类似的错误。这是因为我遗漏了using namespace std;

+0

是的,我想只是输入'#include '是不够的,需要使用命名空间标准; – 2017-07-15 11:48:30