error with printing out stars?

the error is in main where i call PrintChar. it says 'h' is undefined but i thought i defined it with getters and setters. can you tell me where i went wrong? and where to define it? this program is supposed to read in dimensions of rectangles from txt file and print them out in stars

rectangle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#ifndef homework_rectangle_h
#define homework_rectangle_h
class Rectangle {
public:
    Rectangle(double w=1, double h=1);
   
    
    bool operator == (const Rectangle & r) const;
    
    double getWidth();
    void setWidth(double w);
    
    double getHeight();
    void setHeight(double h);
    
    double getArea();
    double getPerimeter();
    void PrintChar(int w = 1, int h = 1, char symbol = '*');
    
    friend std::istream& operator >> (std::istream &iss, Rectangle& r);

    
private:
    double height;
    double width;
};


#endif 


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "rectangle.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

istream& operator >> (istream &iss, Rectangle& r)
{  iss >> r.height >> r.width;
    return iss;
}

int main() {
    cout << r.get_width() << " x " << r.get_height() << endl;
    ifstream in("input.txt"); int numRectangles;
    in >> numRectangles;
    for ( int i = 0; i < numRectangles; i++ ) { Rectangle r;
        in >> r;
        cout << r[0] << " x " << r[1] << endl;
        cout << &r;
    }
    PrintChar(h, w, symbol);
    
    Rectangle r1(4, 5), r2(10, 2); if ( r1 == r2 ) {
        cout << "Both rectangles have the same area!" << endl; }
}


rectangle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "rectangle.h"
#include <iostream>
using namespace std;

Rectangle::Rectangle(double w, double h):width(w),height(h) {}

double Rectangle::getWidth() { return width; }

void Rectangle::setWidth(double w) {width = w; }

double Rectangle::getHeight() { return height; }

void Rectangle::setHeight(double h) { height = h; }

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

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

void Rectangle::PrintChar(int h, int w, char symbol) {
    for (int y = 1; y <= w; y++) {
        for (int x = 1; x <= h; x++) {
            cout << symbol;
        }
        cout << endl;
    }
}

The 'h' and 'w' names are only defined inside the functions 'setHeight' and 'setWidth', but they are not in the rest of your code.

"getters" and "setters" dont define nothing. They are simply functions that return or set values.

From your member function 'PrintChar' you could be using the member variables 'width' and 'height' defined in the 'Rectangle' class.

If you do want to allow a direct specification of a width and height to pass to 'PrintChar', and if you want to use an 'h' and 'w' variables inside main, you'll have to declare them inside main, and then you can set them by using the gets of your rectangle. Or you can use the gets directly in the call to 'PrintChar'.

Also, where is the first 'r' you refer to in main declared?

Topic archived. No new replies allowed.