我在c ++编程方面有点新鲜。我想获得一些关于如何正确添加指向已定义类的指针,以访问对象矩形的内存地址的帮助。 我被要求添加一个指向此代码的指针,我已尝试添加它,但我收到以下错误
预计会员名称。
我不确定问题出在哪里。
class Rectangle
{
public:
float width;
float height;
};
int main()
{
system("color 2b");
cout << "This program will calculate area and perimeter of a rectangle.\n";
Rectangle rect;
Rectangle *ptrrect;
ptrrect = ▭
cout << "Access the member of Rectangle: " << ptrrect->.rect() << endl;
cout << "Please enter the height: \n";
cin >> rect.height;
cout << "Please enter the width: \n";
cin >> rect.width;
cout << "The area of the rectanlge is: " << rect.height * rect.width;
cout << " The premiter of the rectangle is: \n" << (rect.height * 2) + (rect.width * 2);
system("PAUSE");
return 0;
}
答案 0 :(得分:3)
$ make dep && make clean && make
特别是一个问题,因为我没有看到名为ptrrect->.rect()
的函数
同样rect
访问一个尖头对象的成员,而->
只访问该对象的成员,所以如果你应该使用 - &gt;如果您尝试访问的变量whos成员是指针,而.
如果它只是一个对象。
因此,如果您想要.
的宽度,您可以使用rect
或rect.width
ptrrect->width
试图在rect中指向一个名为rect()的函数,该函数不存在。正如我之前所说,如果你在ptrrect->.rect()
.
ptrrect->rect()
class
可能会有用。