如何在C ++中将指针添加到类中

时间:2015-12-02 06:57:29

标签: c++ visual-c++

我在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 = &rect;

    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;
}

1 个答案:

答案 0 :(得分:3)

$ make dep && make clean && make特别是一个问题,因为我没有看到名为ptrrect->.rect()的函数

同样rect访问一个尖头对象的成员,而->只访问该对象的成员,所以如果你应该使用 - &gt;如果您尝试访问的变量whos成员是指针,而.如果它只是一个对象。

因此,如果您想要.的宽度,您可以使用rectrect.width

ptrrect->width试图在rect中指向一个名为rect()的函数,该函数不存在。正如我之前所说,如果你在ptrrect->.rect() .

中有一个名为rect()的函数,ptrrect->rect() class可能会有用。