gpt4 book ai didi

c++ - 错误 : default argument given for parameter 2

转载 作者:行者123 更新时间:2023-12-02 10:04:38 25 4
gpt4 key购买 nike

我正在做一个类(class)项目,我遇到了这些错误,如果有人能提供帮助那就太好了,我是堆栈溢出的新手,所以如果我没有正确发布内容,我深表歉意。错误状态: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 */

这是我的 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;

}

最佳答案

从定义中删除默认参数。而不是

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

改成

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

你得到这个错误的原因是因为你已经在这里的类中声明并定义了绘图函数

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

将此更改为

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

你不会再得到重定义错误了。

关于c++ - 错误 : default argument given for parameter 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60896087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com