简体   繁体   English

代码无法识别“ #include”语句

[英]Code not recognizing “#include” statements

I'm writing some header files for a project, but for whatever reason, I'm getting the error "Identifier ' ' is undefined. What am I doing wrong? (It won't recognize string or Boolean as correct) 我正在为项目编写一些头文件,但是由于某种原因,我会收到错误消息“ Identifier''undefined。我在做什么错?(它不能将字符串或布尔值识别为正确的)

#include <iomanip>
#include <iostream>
#include <string>

class Camper {
private:
    string name;
    boolean paid;
public:
    void setName(string);
    void getName() const;

    void setPaid(boolean);
    void getPaid() const;

    void display() const;
};

Booleans are actually typed as bool in c++ .Also the reason it wont recognize string is that string is part of the std namespace .You either need to add using namespace std; 布尔型实际上是在c++键入为bool 。此外,它无法识别字符串的原因是stringstd namespace一部分using namespace std; under your includes,or else you need to adress string as std::string .Some more elements from the std namespace are Vector,List,etc.You can take a look at those here 在您的include下,否则您需要将string作为std::string地址。std std namespace中的其他元素是Vector,List等。您可以在这里查看这些元素

EDIT:Also I just noticed your getter/setter methods.A getter method is used so you can access Object attributes without them being public,it returns a type of that attribute.If you want to access name described as an std::string your method should return std::string .Meaning your 2 getters should look like this: 编辑:还我刚刚注意到您的getter / setter方法。使用了getter方法,因此您可以在不公开对象属性的情况下访问对象属性,它返回该属性的类型。如果要访问描述为std::string的名称,方法应该返回std::string意味着您的2个getter应该看起来像这样:

bool getPaid() const;
std::string getName() const;

As LF pointed out it is not good practice to use namespaces as it can lead to confusing or even conflicting code.The reference is this 由于LF指出,这是不使用的命名空间很好的做法,因为它可能会导致混乱,甚至相互冲突的code.The引用是这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM