-5

Here is the code that I have so far:

#include <iostream>
using namespace std;

class Rectangle
{
private:
    double width, height;
public:
    Rectangle();
    double Set(double x, double y);
    double getArea();
    double getPerimeter();
};

Rectangle::Rectangle()
{
    width = 1;
    height = 1;
}

double Rectangle::Set(double x, double y)
{
    width = x;
    height = y;
}

double Rectangle::getArea()
{
    double area = height * width;
    return area;
}

double Rectangle::getPerimeter()
{
    double perimeter = (width * 2) + (height * 2);
    return perimeter;
}

int main()
{
    double width, height;
    cout << "Enter the width and height of a rectangle:";
    cin >> width >> height;
    cout << "The area is " << Rectangle::getArea << " and the perimeter is " << Rectangle::getPerimeter << endl;
}

When running this code I get the error : "'Rectangle::getArea': non-standard syntax; use '&' to create a pointer to member" I get the same error about the Rectangle::getPerimeter I am not sure what the problem is, I am new to making classes in C++ obviously, so I am having some trouble. Any suggestions?

samh30
  • 1
  • 3
  • 1
    Should be `rectangle.getArea()` and `rectangle.getPerimeter()`. Note the parentheses: that's how you call a function, even when it has no arguments. – Pete Becker Apr 08 '18 at 16:52
  • 2
    Your class is fine, but you dont use it! You need to create a variable of that type (`Rectangle rect;`), then call member functions ( (`rect.Set(...);`). – kebs Apr 08 '18 at 16:56
  • Too many problems, you should get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) to start with – Killzone Kid Apr 08 '18 at 17:07
  • You should learn about floating point numbers. When you assign to a `double`, prefer to use a floating point constant, such as `1.0`, similarly when multiplying, use `2.0`. – Thomas Matthews Apr 08 '18 at 17:30
  • Your `Set` method lies. The function *signature* says it returns a value. The code doesn't return a value. Maybe you want the return type as `void`. – Thomas Matthews Apr 08 '18 at 17:31
  • Prefer to make methods `const` if they don't modify the data members. Examples: `getArea` and `getPerimeter` don't change the members. This allows the compiler to catch mistakes rather than having the appear during run-time. – Thomas Matthews Apr 08 '18 at 17:33
  • When you call a function, you need to supply `()` and any parameters; otherwise it represents the address of the function. – Thomas Matthews Apr 08 '18 at 17:34

3 Answers3

1

1st create the object of Rectangle() class and then call the respective methods.

Rectangle rect;/* object of rectangle class */

your main() looks like

int main() {
        double width, height;
        cout << "Enter the width and height of a rectangle:";
        cin >> width >> height;
        Rectangle rect;/* object of rectangle class */
        /* calling set method and passing the parameter */
        rect.Set(width,height);
        cout << "The area is " << rect.getArea() << " and the perimeter is " << rect.getPerimeter() << endl;
        return 0;
}

As If i understood correctly you don't need Set() method actually, instead of this you do the same stuff using constructor by passing parameters.

parameterized constructor instead of Set() method

Rectangle::Rectangle(double x, double y) {
        width = x;
        height = y;
}

And create objects like

Rectangle rect(width,height);/* it will be called automatically */
Achal
  • 11,821
  • 2
  • 15
  • 37
1

There are several problems with your code.

1.) You never instantiate the class (i.e. create an object of that class type.) Having two variables with the names width and height is not the same as having an object of the type Rectangle.

2.) You are trying to call a non-static member function as if they were static member functions. Member functions are called via objects, for example rect.getArea() where rect is an object of the type Rectangle.

3.) You are missing the parentheses at the function call. Whenever you get the message non-standard syntax, use & to create a pointer to a member it usually means that you have forgotten the parentheses at a function call.

What you want is probably something like:

int main()
{
    double width, height;
    cout << "Enter the width and height of a rectangle:";
    cin >> width >> height;
    Rectangle rect;
    rect.Set(width, height);
    cout << "The area is " << rect.getArea() << " and the perimeter is " << rect.getPerimeter() << endl;
}
Jim Nilsson
  • 838
  • 4
  • 12
0

For starters this member function returns nothing

double Rectangle::Set(double x, double y)
{
    width = x;
    height = y;
}

So it should be defined like

void Rectangle::Set(double x, double y)
{
    width = x;
    height = y;
}

It seems you mean

//...
cin >> width >> height;
Rectangle r;
r.Set( width, height );

cout << "The area is " << r.getArea() << " and the perimeter is " << r.getPerimeter() << endl;

The functions getArea and getPerimeter should be declared as constant member functions

double getArea() const;
double getPerimeter() const;

because they do not change objects of the type Rectangle.

It would be logical consistent if the class had a constructor with two parameters

Rectangle( double width, double height );

And the method Set should be split into two methods like setWidth and setHeight.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335