3

I am working on classes in C++.

I am basically remaking what I was doing here, but in C++.

It has gone pretty well, but I don't understand what the error member reference type 'Human *' is a pointer; did you mean to use '->'? means. I've never used -> and I've seen * be used in that way (like const char *) but I'm not too sure how it works.

The closest question that I found was this one, but the replies weren't of help.

Here is my code

#include <stdio.h>
#include <cstdlib>
#include <iostream>
using std::cout;
using std::cin;
using std::string;

class Human {
    public:
    string Name;
    int Age;
    double Height;

    void Initialise(string name, int age, double height) {
        this.Name = name; // Error here
        this.Age = age; // Error here
        this.Height = height; // Error here
    }

    void Grow(double rate) {
        if (rate < 0) {
            cout << "You can't grow at a negative rate, silly.\n";
            return;
        }
        else if (rate >= 0.2) {
            cout << "You can't grow that high, silly.\n";
            return;
        }
        this.Height += rate; // Here too
    }
};

int main() {
    return 0;
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
incapaz
  • 349
  • 1
  • 3
  • 11
  • 5
    The error message seems very clear. `this` is a pointer so you need to use `->` instead of `.`. Which part do you need clarified? – François Andrieux Jun 04 '19 at 19:49
  • 3
    If you've never used `->` and written any reasonable amount of C or C++ you life has been truly blessed. – user4581301 Jun 04 '19 at 19:51
  • 3
    Also, learning C++ by try and error is not going to get you to places. Instead, opt for good C++ book: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – SergeyA Jun 04 '19 at 19:51
  • What they said above! It seems you're getting ```->``` and ```.``` confused. You use ```.``` to access variables of a class. But you're using ```this```, so you use ```->```. – Jessica Jun 04 '19 at 19:51
  • Since he beat me to it, I'm going to emphasize what Sergey just said. C++ is a very complicated language, bordering on batsmurf crazy. You can learn it unassisted by trial and error you have hundreds of years at your disposal, but save yourself the time. Get a book. – user4581301 Jun 04 '19 at 19:53
  • In your case, you don't even need to reference `this`. Just say `Name = name` etc. – edtheprogrammerguy Jun 04 '19 at 19:55
  • 1
    To elaborate on the previous comments : C++ cannot be practically self-taught because of Undefined Behavior. In C++ the compiler is not required to (or able to) detect all types of errors, and code that compiles is not necessarily correct. Without referring to formal documentation or training, it's not possible to know if the behavior you observe is guaranteed to be consistent or if your code contains undiagnosed errors where the behavior is just a coincidence. You need a good book or a good teacher to learn C++. – François Andrieux Jun 04 '19 at 19:55
  • 4
    This question is actually pretty well delivered in that the OP doesn't understand what error message means. It has everything needed to answer the question. That the "everything needed" is also in the error message itself is certainly grounds for closure, but I don't think it deserves down-ticks. I could only wish recent new users fo SO presented this much information when posting. It even includes research efforts. So yea, I'm voting to close, but nicely posted regardless. – WhozCraig Jun 04 '19 at 19:57
  • You could use `(*this).Name = name;`. Not what I'd call idiomatic C++, but you can do it that way. – Eljay Jun 04 '19 at 19:58
  • 1
    Technical note: Rather than an `Initialise`function, consider using a [constructor](https://en.cppreference.com/w/cpp/language/initializer_list). With a constructor you can guarantee that there are no uninitialized objects floating around waiting to ruin your day. [That leads to a nifty concept called RAII](https://stackoverflow.com/questions/2321511/what-is-meant-by-resource-acquisition-is-initialization-raii) that can make C++ code much easier to write. – user4581301 Jun 04 '19 at 20:01

1 Answers1

7
  1. this is always a pointer, so you cannot saythis.Name—you would need to say (*this).Name instead.

  2. The syntax a->b is equivalent to (*a).b so you could say this->Name (which is what the error message is explicitly suggesting), although:

  3. Inside methods, this-> is redundant. Usually you can simply refer to, or assign to, Name (although, as Jeremy Friesner points out in his comment, there may be rare/esoteric cases where you might actually want or need it).

  4. As the comments say, C++ must be learned formally. You simply have to know points 1 & 2 (and a million other conventions like them). You can't stumble upon them. Trial and error will not work.

  5. The -> syntax is not even C++ specific. It is part of C. The C rulebook is very slim—so it would be a good idea to formally learn that first before moving on to (and here I can only concur with user4581301) the "batsmurf crazy" complexity of C++.

jez
  • 14,867
  • 5
  • 37
  • 64
  • 1
    Side node re (2): there are times when `this->` is required inside templated methods, for reasons I don't understand well enough to attempt to explain. – Jeremy Friesner Jun 05 '19 at 00:03