1

So here is my code:

//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 //////////////////////

Along with the corresponding .h file:

    // Shapes.h
#ifndef SHAPES_H
#define SHAPES_H

#include "Canvas.h"

///////////////////////////  Shape  ///////////////////////////

class Shape {
public:

  //EFFECTS: creates a shape with initial position (0,0)
  Shape() : xPos(0), yPos(0) {}

  //EFFECTS: returns the area of this Shape
  virtual double area() const = 0;

  //MODIFIES: canvas
  //EFFECTS: draws this shape onto canvas at its current position
  virtual void draw(Canvas *canvas) const {}

  //MODIFIES: xPos, yPos
  //EFFECTS: sets the position of this shape
  void setPosition(double xPos_in, double yPos_in){
    xPos = xPos_in;
    yPos = yPos_in;
  }

  double get_xPos() const { return xPos; }
  double get_yPos() const { return yPos; }

private:

  double xPos; // The x position of this shape
  double yPos; // The y position of this shape

};

/////////////////////////  End Shape  /////////////////////////



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

class Ellipse : public Shape{
public:

  Ellipse();

  //REQUIRES: xRad_in, yRad_in are non-negative
  //EFFECTS: creates an Ellipse with given x and y radii
  Ellipse(double xRad_in, double yRad_in);

  //EFFECTS: returns the area of this Ellipse
  virtual double area() const;

  //MODIFIES: canvas
  //EFFECTS: draws this shape onto canvas
  virtual void draw(Canvas *canvas) const;

private:
  double xRad;  //Half the x-axis of the ellipse
  double yRad;  //Half the y-axis of the ellipse

};

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



///////////////////////////////////////////////////////////////
//               DO NOT MODIFY ABOVE THIS LINE               //
///////////////////////////////////////////////////////////////



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

// PUT YOUR CODE (DECLARATION) FOR CIRCLE HERE
class Circle : public Ellipse{
public:

  //REQUIRES: rad_in is non-negative
  //EFFECTS: creates an Circle with given radius
  Circle(double rad_in);

  //EFFECTS: returns the area of this Circle
  virtual double area() const;

  //MODIFIES: canvas
  //EFFECTS: draws this shape onto canvas
  virtual void draw(Canvas *canvas) const;

private:
  double xRad;  //Radius of the Circle
  double yRad;  //Radius of the Circle

};


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



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

// PUT YOUR CODE (DECLARATION) FOR RECTANGLE HERE
class Rectangle : public Shape{
public:

  //REQUIRES: xRad_in, yRad_in are non-negative
  //EFFECTS: creates an Rectangle with given x and y radii
  Rectangle(double w_in, double h_in);

  //EFFECTS: returns the area of this Rectangle
  virtual double area() const;

  //MODIFIES: canvas
  //EFFECTS: draws this shape onto canvas
  virtual void draw(Canvas *canvas) const;

private:
  double w;  //Length of the Rectangle
  double h;  //Width of the Rectangle

};

//////////////////////// End Rectangle //////////////////////





#endif /* SHAPES_H */

I am supposed to be making Rectangle derived from Shape and Circle derived from Ellipse, both with the corresponding functions that are present in their implementations, and I thought my code had done so, but I got the following compiler error:

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

I really have no idea what's wrong. Please help!

EDIT: Additional Code necessary for compile:

// 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 /////////////////////////

And Canvas.h:

#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 */
Sam
  • 23
  • 1
  • 6
  • Please provide all the code necessary to compile and receive the error. – Neil Kirk Nov 09 '15 at 00:53
  • 1
    It is complaining that there is no default constructor for Ellipse. Make one that puts 0 in the radius values and then see if that helps. – Jerry Jeremiah Nov 09 '15 at 00:57
  • @NeilKirk I added the rest of the code provided to compile. – Sam Nov 09 '15 at 01:00
  • @JerryJeremiah Doesn't C++ take care of making a default constructor? Besides, when I'm making my Circle constructor I want it to take in a parameter, not 0 parameters, and I'm getting 0 as if it thinks I'm giving it 0 parameters. – Sam Nov 09 '15 at 01:01
  • Possible duplicate of [Error : base class constructor must explicitly initialize parent class constructor](http://stackoverflow.com/questions/23647409/error-base-class-constructor-must-explicitly-initialize-parent-class-construct) – Weak to Enuma Elish Nov 09 '15 at 01:01
  • @JamesRoot I tried that and received the error "undefined reference to 'vtable for Circle'" in function Circle::Circle(double) – Sam Nov 09 '15 at 01:09
  • 1
    If you provide any constructor, a default constructor is not automatically provided. Use the initialization list to call a constructor of a base or member. – Neil Kirk Nov 09 '15 at 01:18
  • @NeilKirk Okay, but even with one implemented I'm still getting the error I listed above. (undefined reference) – Sam Nov 09 '15 at 01:23
  • I don't see "undefined reference" above. Also you should paste the error message here instead of linking a picture. Who knows when that URL will expire. – Neil Kirk Nov 09 '15 at 01:24
  • @NeilKirk /tmp/ccQ4iKfZ.o: In function `Circle::Circle(double)': shapes.cpp:(.text+0x1b7): undefined reference to `vtable for Circle' collect2: error: ld returned 1 exit status – Sam Nov 09 '15 at 01:26
  • As an aside, I'm not sure what the purpose of the Circle class is. Just make a 1 parameter constructor of Ellipse for creating Circles. – Neil Kirk Nov 09 '15 at 01:26
  • Have you implemented all your virtual functions? Where is your virtual destructor for Shape? – Neil Kirk Nov 09 '15 at 01:28
  • @NeilKirk I updated my code for the .cpp and .h files for shapes to represent how I handled the constructors. I need to implement Circle because it's outlined in the spec I have to follow; it must take one double as an argument as I have it. – Sam Nov 09 '15 at 01:32

1 Answers1

0

Circle inherits from Ellipse, but Ellipse does not have a default constructor. Therefore either provide one, or call the desired constructor of Ellipse in the initialization list of Circle's constructor.

Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • I did, I changed my code to "Circle::Circle(double rad_in) : Ellipse(rad_in, rad_in) {}" for the implementation for Circle's constructor – Sam Nov 09 '15 at 01:24