Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 多态性&;派生类_C++_Class_Compiler Errors_Polymorphism_Derived Class - Fatal编程技术网

C++ 多态性&;派生类

C++ 多态性&;派生类,c++,class,compiler-errors,polymorphism,derived-class,C++,Class,Compiler Errors,Polymorphism,Derived Class,这是我的代码: //Shapes.cpp #include <cassert> #include <cmath> #include "shapes.h" using namespace std; const double PI = 3.14159; ////////////////////////// Ellipse ////////////////////////// Ellipse::Ellipse() : xRad(0), yRad(0){} Elli

这是我的代码:

//Shapes.cpp
#include <cassert>
#include <cmath>
#include "shapes.h"

using namespace std;

const double PI = 3.14159;

//////////////////////////  Ellipse  //////////////////////////

Ellipse::Ellipse() : xRad(0), yRad(0){}

Ellipse::Ellipse(double xRad_in, double yRad_in)
  : xRad(xRad_in), yRad(yRad_in) {}

double Ellipse::area() const {
  return PI * xRad * yRad;
}

void Ellipse::draw(Canvas *canvas) const{
  // Iterate through the grid of (x,y) pixel coordinates
  // in the canvas.
  for(int x = 0; x < CANVAS_WIDTH; ++x){
    for(int y = 0; y < CANVAS_HEIGHT; ++y){

      // The ellipse contains the point (x,y) if and only if
      // ((x-xPos)/xRad)^2 + ((y-yPos)/yRad)^2 <= 1

      double xDiff = x - get_xPos();
      double yDiff = y - get_yPos();

      if( (xDiff/xRad)*(xDiff/xRad) + (yDiff/yRad)*(yDiff/yRad) <= 1 ){
        // If the pixel is contained in the ellipse, set it to true
        canvas->setPixel(x, y, true);
      }
    }
  }
}

///////////////////////// End Ellipse /////////////////////////



//////////////////////////  Circle  //////////////////////////

// PUT YOUR CODE (IMPLEMENTATIONS) FOR CIRCLE HERE
Circle::Circle(double rad_in)
  : Ellipse(rad_in, rad_in) {}

//Use Ellipse's area function by sending it the radius of the
//circle for the xRad and yRad parameters

//Use Ellipse's draw function


///////////////////////// End Circle /////////////////////////



////////////////////////  Rectangle  /////////////////////////

// PUT YOUR CODE (IMPLEMENTATIONS) FOR RECTANGLE HERE
Rectangle::Rectangle(double w_in, double h_in)
  : w(w_in), h(h_in) {}

double Rectangle::area() const {
  return w * h;
}

void Rectangle::draw(Canvas *canvas) const{
  // Iterate through the grid of (x,y) pixel coordinates
  // in the canvas.
  for(int x = 0; x < CANVAS_WIDTH; ++x){
    for(int y = 0; y < CANVAS_HEIGHT; ++y){

      // The Rectangle contains the point (x,y) if and only if
      // ((x-xPos)/xRad)^2 + ((y-yPos)/yRad)^2 <= 1

      double xDiff = x - get_xPos();
      double yDiff = y - get_yPos();

      if( abs(xDiff) <= w/2 && abs(yDiff) <= h/2 ){
        // If the pixel is contained in the Rectangle, set it to true
        canvas->setPixel(x, y, true);
      }
    }
  }
}

//////////////////////// End Rectangle //////////////////////
我应该制作从形状派生的矩形和从椭圆派生的圆,这两个函数的实现中都有相应的函数,我认为我的代码已经这样做了,但是我得到了以下编译器错误:

shapes.cpp: In constructor \u2018Circle::Circle(double)\u2019:
shapes.cpp:47:30: error: no matching function for call to \u2018Ellipse::Ellipse()\u2019
   : xRad(rad_in), yRad(rad_in) {}
                              ^
shapes.cpp:47:30: note: candidates are:
shapes.cpp:12:1: note: Ellipse::Ellipse(double, double)
 Ellipse::Ellipse(double xRad_in, double yRad_in)
 ^
shapes.cpp:12:1: note:   candidate expects 2 arguments, 0 provided
In file included from shapes.cpp:4:0:
shapes.h:45:7: note: Ellipse::Ellipse(const Ellipse&)
 class Ellipse : public Shape{
       ^
shapes.h:45:7: note:   candidate expects 1 argument, 0 provided
我真的不知道怎么了。请帮忙

编辑:编译所需的附加代码:

// Canvas.cpp    
#include <iostream>
    #include <cassert>
    #include "Canvas.h"

using namespace std;

/////////////////////////  Canvas  ///////////////////////////

Canvas::Canvas(){
  for(int row = 0; row < CANVAS_HEIGHT; ++row){
    for(int col = 0; col < CANVAS_WIDTH; ++col){
      grid[row][col] = false;
    }
  } 
}

void Canvas::setPixel(int x, int y, bool value){
  assert(0 <= x); assert(x < CANVAS_WIDTH);
  assert(0 <= y); assert(y < CANVAS_HEIGHT);
  grid[y][x] = value;
}

void Canvas::print() const {
  for(int row = 0; row < CANVAS_HEIGHT; ++row){
    for(int col = 0; col < CANVAS_WIDTH; ++col){
      cout << (grid[CANVAS_HEIGHT-row-1][col] ? PIXEL_ON : PIXEL_OFF) << " ";
    }
    cout << endl;
  } 
}

////////////////////////// End Canvas /////////////////////////
//Canvas.cpp
#包括
#包括
#包括“Canvas.h”
使用名称空间std;
/////////////////////////帆布///////////////////////////
Canvas::Canvas(){
对于(int row=0;row断言(0
继承自
椭圆
,但
椭圆
没有默认构造函数。因此,请提供一个构造函数,或在
的构造函数的初始化列表中调用所需的
椭圆
构造函数。

继承自
椭圆
,但
Ellipse
没有默认构造函数。因此,请提供一个,或者在
Circle
的构造函数的初始化列表中调用所需的
Ellipse
构造函数。

请提供编译和接收错误所需的所有代码。它抱怨Ellipse没有默认构造函数。请一个将半径值设为0,然后看看是否有帮助。@ Neelkrk,我添加了编译的代码。@ JerryJeremiah不C++做默认构造函数吗?此外,当我做我的圆构造函数时,我希望它能取一个参数,而不是0个参数,而我得到0,好像它认为我给它0个参数。ters。如果提供任何构造函数,则不会自动提供默认构造函数。请使用初始化列表调用基或成员的构造函数。请提供编译和接收错误所需的所有代码。它抱怨椭圆没有默认构造函数。创建一个在半径值和Nekkrk,我补充了编译所提供的代码。@ JerryJeremiah不C++做默认构造函数吗?此外,当我做我的圆构造函数时,我想它取一个参数,而不是0个参数,而我得到0,好像它认为我给它0个参数。lt构造函数不是自动提供的。使用初始化列表调用基或成员的构造函数。我这样做了,我将代码更改为“Circle::Circle(double rad_in):Ellipse(rad_in,rad_in){}”。对于Circle的构造函数的实现,我将代码更改为“Circle::Circle(double rad_in):Ellipse(rad_in,rad_in){}”用于实现Circle的构造函数
// Canvas.cpp    
#include <iostream>
    #include <cassert>
    #include "Canvas.h"

using namespace std;

/////////////////////////  Canvas  ///////////////////////////

Canvas::Canvas(){
  for(int row = 0; row < CANVAS_HEIGHT; ++row){
    for(int col = 0; col < CANVAS_WIDTH; ++col){
      grid[row][col] = false;
    }
  } 
}

void Canvas::setPixel(int x, int y, bool value){
  assert(0 <= x); assert(x < CANVAS_WIDTH);
  assert(0 <= y); assert(y < CANVAS_HEIGHT);
  grid[y][x] = value;
}

void Canvas::print() const {
  for(int row = 0; row < CANVAS_HEIGHT; ++row){
    for(int col = 0; col < CANVAS_WIDTH; ++col){
      cout << (grid[CANVAS_HEIGHT-row-1][col] ? PIXEL_ON : PIXEL_OFF) << " ";
    }
    cout << endl;
  } 
}

////////////////////////// End Canvas /////////////////////////
#ifndef CANVAS_H
#define CANVAS_H

/////////////////////////  Canvas  ///////////////////////////

//Canvas Constants
const int CANVAS_WIDTH = 30;
const int CANVAS_HEIGHT = 30;

const char PIXEL_ON = '#';
const char PIXEL_OFF = ' ';

class Canvas {
  //OVERVIEW:  A Canvas object represents a 2D grid of "pixels"
  //           which can be set to either "on" or "off".  A Canvas
  //           knows how to print itself out to the terminal.  The
  //           canvas has a fixed width and height and the origin
  //           (0,0) of the canvas's coordinate system is at the
  //           bottom left.

public:

  //EFFECTS: creates a new Canvas with size CANVAS_WIDTH x CANVAS_HEIGHT
  Canvas();

  //REQUIRES: the pixel is on the canvas (0 <= x < CANVAS_WIDTH, 0 <= y < CANVAS_HEIGHT)
  //MODIFIES: grid
  //EFFECTS: if value is true, turns the pixel at (x,y) on
  //         if value is false, turns the pixel at (x,y) off
  void setPixel(int x, int y, bool value);

  //EFFECTS: prints this canvas to cout
  void print() const;

private:

  bool grid[CANVAS_HEIGHT][CANVAS_WIDTH];

};

////////////////////////// End Canvas /////////////////////////



#endif /* CANVAS_H */