-2

I have been tasked to calculate the area and perimeter of a rectangle using class. The input function is supposed to be inside class. I wrote code so far but there seem to be errors I can't detect.

Some help would be appreciated.

rectangle.h

#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle{
    public:
        int width, length;
        Rectangle();
        Rectangle(int width1,int length1);
        void getRectangle();
        int getArea();
        int getPerimeter();
};

#endif // RECTANGLE_H

rectangle.cpp

// oporer ta class

#include<iostream>
#include "rectangle.h"
Rectangle::Rectangle()
{
    width=0;
    length=0;
}
Rectangle::Rectangle(int width1,int length1)
{
    width=width1;
    length=length1;
}
void Rectangle::getRectangle()
{
    cin>>width>>length;
}
int Rectangle::getArea()
{
    return (width*length);
}
int Rectangle::getPerimeter()
{
    return (width+length)*2
}
// oporer ta rectangle cpp 

main.cpp

#include <iostream>
#include "rectangle.h"
using namespace std;

int main()
{
    Rectangle paraFirst();
    paraFirst.getRectangle();
    return 0;
}
// main fucntion
drescherjm
  • 10,365
  • 5
  • 44
  • 64
Fahim Hassan
  • 61
  • 1
  • 9
  • it's a bit lengthy, so i thought it would be better to post on another website, but im trying to add the code here now – Fahim Hassan May 13 '20 at 16:50
  • 1
    You can't just copy-paste code from multiple files into an online IDE, the `#include`s wont work and obscure the actual error. But note that `Rectangle paraFirst();` will be parsed as function declaration, not as constructor call. – Lukas-T May 13 '20 at 16:51
  • Have you seen the code now? – Fahim Hassan May 13 '20 at 17:11
  • Your question would be better if you added the text of the errors. If this is Visual Studio the Output Tab is a better choice to copy the text of the error messages from than the Errors List. The reason is the output tab is more verbose and its 100% text not a separated list. – drescherjm May 13 '20 at 17:16

1 Answers1

1

Assuming that the #includes work in your local setup, I see two typos here:

  • cin>>width>>length; must std::cin >> width >> length;
  • missing semicolon after return (width+length)*2

And probably the main problem:

Rectangle paraFirst();

is parsed as a declaration for a function that takes no arguments and returns a Rectangle. See also Most vexing parsing. To call the default constructor simply use

Rectangle paraFirst;    

or

Rectangle paraFirst{};
Lukas-T
  • 11,133
  • 3
  • 20
  • 30