0

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)

#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;
};
webbern
  • 19
  • 2
  • 6
    It's `std::string` and `bool` – Igor Tandetnik Apr 30 '19 at 03:10
  • Alternate: on the line after "#include < string >" add "using std::string;". A comma list can be used for std=c++17 or later, such as "using std::string, std::to_string;". Avoid "using namespace std;". – 2785528 Apr 30 '19 at 03:23
  • Aren't you missing using namespace std; – Adan Vivero Apr 30 '19 at 04:19
  • 3
    @AdanVivero writing `using namespace std;` in the header is a really bad idea. – t.niese Apr 30 '19 at 05:05
  • @t.niese why? I do it all the time, and nothing bad happens. – Adan Vivero Apr 30 '19 at 05:05
  • 2
    @AdanVivero You have gotten lucky. Sooner or later you will have an identifier with the same name as something in the std namespace and you'll get a really nasty shock. [More on that here.](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) Putting `using namespace std` in a header spreads that pain to anyone who includes the header. – user4581301 Apr 30 '19 at 05:43
  • @AdanVivero You should not use `using namespace` in the header in general, and especially not for `std`. The `std` namespace contains a huge amount of function names that are commonly used in other libraries as well. Namespaces exists to minimize the risk of naming conflicts. If you now place a `using namespace` in a header, then all file that directly or indirectly include that header loose that protection, and in combination with other libraries it can result in hard to debug compile errors. – t.niese Apr 30 '19 at 08:06

1 Answers1

1

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; 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

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:

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

As L.F. pointed out it is not good practice to use namespaces as it can lead to confusing or even conflicting code.The reference is this

laegirl
  • 144
  • 13
  • 5
    [I would not recommend `using namespace std;`.](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – L. F. Apr 30 '19 at 03:19
  • Thats a good point,I didn't want to overwhelm him with information.I will edit that in just in case @L.F. – laegirl Apr 30 '19 at 03:21