3

I am working on a class project and im getting these errors, if anyone could possibly help that would be great, im new to stack overflow so im sorry if i dont post things properly. The error states :Shapes.cpp:35:51: error: default argument given for parameter 2 of 'bool Point::draw(Screen&, char)' [-fpermissive] bool Point::draw(Screen& scr, char ch = Screen::FG){

#ifndef Shapes_h
#define Shapes_h
#include<bits/stdc++.h>
using namespace std;

class Screen{
    friend class Shape;
private:
    size_t _w, _h;
    vector<vector<char> > _pix;

public:
    static const char FG = '*', BG = '.';
    Screen(size_t w, size_t h){
        _pix.resize(_h, vector<char>(_w));
    }
    size_t get_w() const{ return _w;}
    size_t get_h() const{ return _h;}
    vector<vector<char> >& get_pix() { return _pix;}

    void set_w(size_t w) { _w = w;}
    void set_h(size_t h) { _h = h;}

    void clear() { fill(BG); }
    void fill(char c); // todo cpp file
    string to_string() const;

    friend std::ostream& operator<<(std::ostream& os, const Screen& scr) {
        return os << scr.to_string();
    };

    friend class Tests; // Don't remove this line
};

// SHAPE ---------------

class Shape{
public:
    virtual ~Shape() {}
    virtual bool draw(Screen& scr, char ch = Screen::FG) = 0;
    friend class Tests;
};

class Point : public Shape {
private:
    size_t _x, _y;

public:
    Point(size_t x, size_t y) : _x(x), _y(y) {}
    virtual ~Point() {}
    bool draw(Screen& scr, char ch = Screen::FG);
    friend class Tests;
};

class Line: public Shape{
private:
    size_t _x1, _y1, _x2, _y2;

    static bool draw_by_x(Screen& scr, char ch, size_t x1, size_t y1, size_t x2, size_t y2){
        if (x2>x1){
            draw_by_x(scr, ch, x2, y2, x1, y1);
        }
        double dy = (double) ((double)y2-y1)/((double)x2-x1);
        size_t x = x1;
        size_t y = y1;
        bool boo = true;
        for (size_t i = x1; i <= x2; i++){
            boo &= Point((size_t)x, (size_t) y).draw(scr, ch);
            x++; y += dy;
        }
        return boo;
    }
    static bool draw_by_y(Screen& scr, char ch, size_t x1, size_t y1, size_t x2, size_t y2){
        if (x2>x1){
            draw_by_x(scr, ch, x2, y2, x1, y1);
        }

        double dx = (double) ((double)x2-x1)/((double)y2-y1);
        size_t x = x1;
        size_t y = y1;
        bool boo = true;
        for (size_t i = x1; i <= x2; i++){
            boo &= Point((size_t)x, (size_t) y).draw(scr, ch);
            x += dx; y += 1;
        }
        return boo;
    }

public:
    Line(size_t a, size_t b, size_t c, size_t d) : _x1(a), _y1(b), _x2(c), _y2(d) {}
    virtual ~Line() {}
    bool draw(Screen& scr, char ch = Screen::FG);
    friend class Tests; // Don't remove
};

class Quadrilateral: public Shape {
private:
    size_t _x1, _y1, _x2, _y2, _x3, _y3, _x4, _y4;

public:
    Quadrilateral(size_t a, size_t b, size_t c, size_t d, size_t e, size_t f, size_t g, size_t h):
    _x1(a), _y1(b), _x2(c), _y2(d), _x3(e), _y3(f), _x4(g), _y4(h) {}

    virtual ~Quadrilateral() {}
    bool draw(Screen& scr, char ch = Screen::FG);

    friend class Tests; // Don't remove
};

class Upright_Rectangle : public Quadrilateral {

public:
    Upright_Rectangle(size_t x1,size_t y1, size_t x2, size_t y2) :
    Quadrilateral(x1,y1, x1,y2, x2,y2, x2,y1) {}
    virtual ~Upright_Rectangle() {}
};

class Stick_Man : public Shape{
    static const size_t DEFAULT_W = 20, DEFAULT_H = 40;

private:
    size_t _x, _y, _w, _h;
    vector < Shape* > _parts;

public:
    Stick_Man(size_t x = 0, size_t y = 0, size_t w = DEFAULT_W, size_t h = DEFAULT_H){
    }

    virtual ~Stick_Man();
    const std::vector<Shape *>& get_parts() const { return _parts; }
    bool draw(Screen& scr, char ch = Screen::FG){
        return"";
    }

    friend class Tests; // Don't remove
};

#endif /* Shapes_h */

HERE IS MY CPP:

#include "Shapes.h"
#include<bits/stdc++.h>
#define pb push_back
using namespace std;

void Screen::fill(char c){
    for (size_t i = 0; i < _h; i++){
        for (size_t j = 0; j < _w; j++){
            _pix[i][j] = c;
        }
    }
    return;
}

void Screen::clear(char c){
    fill(c);
    return;
}



string Screen::to_string()const{
    string ans = "";
    for (size_t i = _h-1; i >= 0; i--){
        for (size_t j = 0; j < _w; j++){
            ans += _pix[i][j];
        }
        ans += "\n";
    }
    return ans;
}



bool Point::draw(Screen& scr, char ch = Screen::FG){
    if (_x < scr.get_h() && _y < scr.get_w()){
        scr.get_pix()[_x][_y] = ch;
        return true;
    }

    return false;
}

bool Line::draw(Screen& scr, char ch = Screen::FG){
    if (abs(int(_x1-_x2)) > abs(int(_y1-_y2))) return draw_by_x(scr, ch, _x1, _y1, _x2, _y2);
    return draw_by_y(scr, ch, _x1, _y1, _x2, _y2);
}

bool Quadrilateral::draw(Screen& scr, char ch = Screen::FG){
    size_t x1, y1, x2, y2, x3, y3, x4, y4;
    x1 = _x1;
    x2 = _x2;
    x3 = _x3;
    x4 = _x4;
    y1 = _y1;
    y2 = _y2;
    y3 = _y3;
    y4 = _y4;

    if ((x2-x3)==(x4-x1) && (y2-y3)==(y4-y1)){
        swap(x2, x3);
        swap(y2, y3);
    }
    else if ((x4-x3)==(x2-x1) && (y4-y3)==(y2-y1)){
        swap(x4, x3);
        swap(y4, y3);
    }

    bool boo = true;
    boo &= Line(x1, y1, x2, y2).draw(scr, ch);
    boo &= Line(x2, y2, x3, y3).draw(scr, ch);
    boo &= Line(x3, y3, x4, y4).draw(scr, ch);
    return boo;

}
seanp
  • 31
  • 1
  • 2
  • Shoving "`using namespace std;`" ***into a header file*** is just [asking for a world of hurt](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). You will really do yourself a big favor if you completely forget that "using namespace std;" is a part of C++, and learn how to use namespaces correctly. – Sam Varshavchik Mar 28 '20 at 01:04
  • 2
    The default value only goes in the header (with the declaration) not the cpp file (with teh definition). – 1201ProgramAlarm Mar 28 '20 at 01:13
  • @1201ProgramAlarm I Used your thought and it helped clear up some errors im still getting this one though, thank you for your help! If there were build errors, you can see the first 10 lines below. Shapes.cpp:15:6: error: prototype for 'void Screen::clear(char)' does not match any in class 'Screen' void Screen::clear(char c){ ^~~~~~ In file included from Shapes.cpp:1:0: Shapes.h:24:7: error: candidate is: void Screen::clear() void clear() { fill(BG); } ^~~~~ – seanp Mar 28 '20 at 01:30

1 Answers1

5

Remove the default parameter from the definition. Instead of

bool Point::draw(Screen& scr, char ch = Screen::FG){
    if (_x < scr.get_h() && _y < scr.get_w()){
        scr.get_pix()[_x][_y] = ch;
        return true;
    }

Change it to

bool Point::draw(Screen& scr, char ch){
    if (_x < scr.get_h() && _y < scr.get_w()){
        scr.get_pix()[_x][_y] = ch;
        return true;
    }

The reason you get the error is because you've already declared and defined it the draw function within the class here

bool draw(Screen& scr, char ch = Screen::FG){
    return"";
}

Change this to

bool draw(Screen& scr, char ch = Screen::FG){
}

And you won't get the redefinition error anymore.

pctopgs
  • 439
  • 1
  • 4
  • 10