1

I'm currently defining a few properties for a class in C++ but I'm running into trouble when using type string as opposed to something like int or double. For example:

private:
    int LOT;

public:
    int getLOT() {
        return LOT;
    }

    void setLOT(int value) {
        LOT = value;
    }

works fine, but:

private:
    string name;

public:
    string getName() {
        return name;
    }

    void setName(string value) {
        name = value;
    }

throws these errors:

https://s26.postimg.org/wm5y7922h/error.png

The file (a header) looks something like this:

#include "general.h" // a header which includes all my other #includes
// which, yes, does include <string>

class MyClass
{
private:
    string name;

public:
    string getName() {
        return name;
    }

    void setName(string value) {
        name = value;
    }

    // other properties similar to the above
}

The purpose is to access the variable like this:

cout << "Enter your name: ";
cin >> MyClass.setName();
cout << "\nHello, " << MyClass.getName();
// although this isn't exactly how it'll be used in-program

If anyone could provide help with what I'm doing wrong or a better way to go about a string property (as, like I mentioned before, other types work fine) it would be greatly appreciated. Thanks.

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • 2
    `std::string`?... – Chris Dodd Sep 09 '17 at 04:12
  • @ChrisDodd, changing the first appearance of `string` in the line to `std::string` as suggested removed all the errors from the line. Thank you. As just an additional curiosity, 1) why don't I have to add on `std::` to the other instances of `string` and, 2) I'm `using namespace std;` (from my `general.h`), so why do I have to repeat the `std::`? New to C++ so please ignore me if I sound stupid. EDIT: upon further testing, I did in fact have to add `std::` to the other instances of `string`. 2) still stands though; are they not the same? – Anthony Hackett Sep 09 '17 at 04:17
  • Keep an eye on `cin >> MyClass.setName();` It will bite before long. – user4581301 Sep 09 '17 at 04:22
  • You need a better understanding of error message from the compiler and, in my own opinion, your tool is not helping you very much. In the error list, the most important (other errors depends on it), in this case, is the last one which will probably point you to name member declaration. The problem I often face using visual studio error window is that it sorts errors so that you don't have them in the same seuence they are fired out. I generally use output window to know what the actual output from the compiler is, then eventually revert to error window in case there is a mess of messages. – Giuseppe Puoti Sep 11 '17 at 16:15

1 Answers1

2

string is part of std namespace.

You must use std::string instead of string or add using namespace std; (what I would not recommend you to do in your header file, read "using namespace" in c++ headers).

jpo38
  • 20,821
  • 10
  • 70
  • 151
  • Thanks -- @ChrisDodd also helped with this. Just a little inquiry: since you personally don't recommend adding `using namespace std;` in my header file, does that mean I have to use `std::string` as opposed to `string` every time? Why don't you recommend it? New to C++ so if my question is silly you can ignore it. Thank you for your help. – Anthony Hackett Sep 09 '17 at 04:23
  • @AnthonyHackett: Prefix ̀string` by ̀std::string` in the header and add `using namespace std;` to the implementation file. See https://stackoverflow.com/questions/5849457/using-namespace-in-c-headers. – jpo38 Sep 09 '17 at 04:26
  • @AnthonyHackett Anyone including such a header gets a nasty surprise when stuff they think is properly fenced by the namespace suddenly isn't. For example, their `reverse` function is now in competition with `std::reverse`. Mystery bugs galore may ensue. – user4581301 Sep 09 '17 at 04:27
  • @user4581301 is there a better way to do it, then? Or will I just have to squash the bugs as they come? – Anthony Hackett Sep 09 '17 at 04:28
  • @AnthonyHackett In general, [Why is “using namespace std” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Keep `using namespace` tightly scoped and only use it where you know it is safe.You can also pick and choose what you want from a namespace, `using std::cin;`, for example, allows you to use `cin` without getting sideswiped by `swap` or `find`. – user4581301 Sep 09 '17 at 04:32