【发布时间】:2019-10-20 08:52:25
【问题描述】:
我正在使用 C++ 编写课程。
我基本上是在改造我正在做的here,但在 C++ 中。
进展顺利,但我不明白错误member reference type 'Human *' is a pointer; did you mean to use '->'? 是什么意思。我从来没有用过->,我也见过*以这种方式使用(比如const char *),但我不太确定它是如何工作的。
我找到的最接近的问题是this 一个,但回复没有帮助。
这是我的代码
#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;
}
【问题讨论】:
-
错误信息看起来很清楚。
this是一个指针,因此您需要使用->而不是.。你需要澄清哪一部分? -
如果您从未使用过
->并编写过任何合理数量的 C 或 C++,那么您的生活真的很幸福。 -
另外,通过尝试和错误来学习 C++ 并不会让你到达目的地。相反,选择好的 C++ 书籍:stackoverflow.com/questions/388242/…
-
详细说明之前的 cmets:由于未定义的行为,C++ 实际上无法自学。在 C++ 中,编译器不需要(或能够)检测所有类型的错误,并且编译的代码不一定正确。如果不参考正式的文档或培训,就不可能知道您观察到的行为是否保证是一致的,或者您的代码是否包含未诊断的错误,而这些行为只是巧合。学习 C++ 需要一本好书或一位好老师。
-
这个问题实际上已经很好地传递了,因为 OP 不理解错误消息的含义。它拥有回答问题所需的一切。 “所需的一切”也在错误消息本身中当然是关闭的理由,但我认为它不应该被贬低。我只希望最近的新用户在发帖时能提供这么多信息。它甚至包括研究工作。所以是的,我投票结束,但无论如何都很好地发布了。