我正在尝试使用显示函数来保存浮点数的信息,当我尝试运行程序时,它会给出错误"非标准语法;使用'&"创建指针成员"然后我添加'&'浮点数并使它们成为指针,它给出了错误"'&&#39 ;:对绑定成员函数表达式的非法操作。我不知道为什么它不会工作。
继承我的代码:
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class Rectangle
{
public:
//constructor
Rectangle() { }
//accessor
float GetHeight() { return m_height; } const
float GetWidth() { return m_width; } const
float GetArea() { return m_height * m_width; }
float GetPerimeter() { return m_height + m_height + m_width + m_width; }
//mutator
void SetHeight(float h)
{
cout << "Please Enter height: \n";
cin >> h;
};
void SetWidth(float w)
{
cout << "Please Enter the width: \n";
cin >> w;
}
void Display()
{
cout << "Height: " << GetHeight << " " << "Width: " << GetWidth << " " << "Area: " << GetArea << " " << "Perimeter: " << GetPerimeter << endl;
};
private:
float m_height;
float m_width;
};
using namespace std;
int main()
{
Rectangle r;
float height = 0;
float width = 0;
r.SetHeight(height);
r.SetWidth(width);
r.Display();
_getch();
return 0;
}
答案 0 :(得分:4)
GetHeight不是变量,而是函数,添加括号:
void Display()
{
cout << "Height: " << GetHeight() << " " << "Width: " << GetWidth() << " " << "Area: " << GetArea() << " " << "Perimeter: " << GetPerimeter() << endl;
};
答案 1 :(得分:0)
除了gabonator所说的,你可能还想设置对象的值:
void SetHeight(float h)
{
cout << "Please Enter height: \n";
cin >> h;
m_height = h;
};