标题中的#include <string>定义了一些结构.错误:字符串未定义类型

Mat*_*son 4 c++ struct types header include

#ifndef STRCUTS_H
#define STRCUTS_H
#include <string>

struct menuEntry 
{ 
    string itemID;    //'string' does not name a type
    string itemName;  //'string' does not name a type
};

#endif
Run Code Online (Sandbox Code Playgroud)

当我把#include <string>放在标题保护之上时,我得到了同样的错误.想想看,我之前在结构中定义结构定义时遇到了奇怪的麻烦.必须是我没有得到的东西.

Pau*_*l R 7

你需要换stringstd::string,即

#ifndef STRCUTS_H
#define STRCUTS_H

#include <string>

struct menuEntry 
{ 
    std::string itemID;
    std::string itemName;
};

#endif
Run Code Online (Sandbox Code Playgroud)

  • @KitsuneYMG:不 - 你永远不应该在头文件中使用`using namespace std`(或`using namespace <anything>`. (4认同)