我的程序如下:
#include "iostream"
using namespace std;
class Crectangle{
public:int w, h;
void Init(int _w,int _h) {
w = _w;
h = _h;
}
int Area() {
return w*h;
}
int Perimeter() {
return 2 * (w + h);
}
};
int main() {
int w=0, h=0;
cin >> w >> h;
Crectangle r;
r.Init(w,h);
cout <<r.Area<< endl <<r.Perimeter;
return 0;
}
报错是 non-standard syntax; use '&' to create a pointer to member。怎么解决?
Area() and Perimeter() are both functions, brother. To call a function, please add () after the function name
I guess there is something wrong with
cout <<r.Area<< endl <<r.Perimeter;
, try changing it tocout <<r.Area()<< 'n' <<r.Perimeter();
.In addition, the constructor can be used for initialization.