I'll try to keep this simple, i have an abstract class called Shape (Shape.h) which has a few pure virtual functions, cool.
so I have another class called Circle (Circle.h and Circle.cpp) which extends Shape.
here's the code.
(In Shape.h)
#ifndef SHAPE_H
#define SHAPE_H
#include "Point.h"
class Shape { // ABC
public:
virtual ~Shape() {}
virtual void draw() const = 0;
#if 1
/*** transformations ***/
// translate by amount delta, e.g. if delta is (1,2), the point (3,4)
// is transformed to (4,6)
virtual void translate(const Point& delta) = 0;
// reflection about x-axis, e.g. (1,2) -> (1,-2)
virtual void xreflect() = 0;
// reflection about y-axis, e.g. (1,2) -> (-1,2)
virtual void yreflect() = 0;
// translate by amount delta, e.g. if delta is (1,2), the point (3,4)
// is transformed to (4,6)
virtual void unTranslate(const Point& delta) = 0;
// reflection about x-axis, e.g. (1,2) -> (1,-2)
virtual void unXreflect() = 0;
// reflection about y-axis, e.g. (1,2) -> (-1,2)
virtual void unYreflect() = 0;
#endif
};
#endif
(In Circle.h)
#ifndef CIRCLE_H
#define CIRCLE_H
#include <iostream>
#include <string>
#include "Shape.h"
#include "Point.h"
class Circle: public Shape {
public:
explicit Circle(std::istream& is);
virtual void draw() const;
// translate by amount delta, e.g. if delta is (1,2), the point (3,4)
// is transformed to (4,6)
virtual void Shape::translate(const Point& delta);
// reflection about x-axis, e.g. (1,2) -> (1,-2)
virtual void Shape::xreflect();
// reflection about y-axis, e.g. (1,2) -> (-1,2)
virtual void Shape::yreflect();
// translate by amount delta, e.g. if delta is (1,2), the point (3,4)
// is transformed to (4,6)
virtual void Shape::unTranslate(const Point& delta);
// reflection about x-axis, e.g. (1,2) -> (1,-2)
virtual void Shape::unXreflect();
// reflection about y-axis, e.g. (1,2) -> (-1,2)
virtual void Shape::unYreflect();
private:
Point centre_;
int radius_;
};
#endif
(In Circle.cpp)
#include "stdafx.h"
#include "Circle.h"
#include <cmath>
using namespace std;
Circle::Circle(istream& is) {
is >> centre_ >> radius_;
if (!is)
throw "Circle::Circle(const std::string&)";
}
void
Circle::draw() const {
cerr << "[C: " << centre_ << ", " << radius_ << "]" << endl;
}
// translate by amount delta, e.g. if delta is (1,2), the point (3,4)
// is transformed to (4,6)
void Circle::translate(const Point& delta)
{
centre_.setX(centre_.getX() + delta.getX());
centre_.setY(centre_.getY() + delta.getY());
}
// reflection about x-axis, e.g. (1,2) -> (1,-2)
void Circle::xreflect()
{
centre_.setY(-abs(centre_.getY()));
}
// reflection about y-axis, e.g. (1,2) -> (-1,2)
void Circle::yreflect()
{
centre_.setX(-abs(centre_.getX()));
}
// translate by amount delta, e.g. if delta is (1,2), the point (3,4)
// is transformed to (4,6)
void Circle::unTranslate(const Point& delta)
{
centre_.setX(centre_.getX() - delta.getX());
centre_.setY(centre_.getY() - delta.getY());
}
// reflection about x-axis, e.g. (1,2) -> (1,-2)
void Circle::unXreflect()
{
centre_.setY(abs(centre_.getY()));
}
// reflection about y-axis, e.g. (1,2) -> (-1,2)
void Circle::unYreflect()
{
centre_.setX(abs(centre_.getX()));
}
I also have a Shape Factory class (ShapeFactory.h) which creates and returns an object based on the parameter you give it.
#ifndef SHAPEFACTORY_H
#define SHAPEFACTORY_H
#include <iostream>
#include <string>
#include "Shape.h"
#include "Circle.h"
#include "Triangle.h"
class ShapeFactory {
public:
ShapeFactory(std::istream& is): in_(&is) {}
Shape* create() {
std::string type;
if (!(*in_ >> type))
return 0;
if (type == "C")
return new Circle(*in_);
if (type == "T")
return new Triangle(*in_);
return 0; // if it's not one of the valid types
}
private:
std::istream *in_;
};
#endif
I've done this entire project in Visual Studio 2010, compiles with no errors and warnings, however, when i try to compile with gcc (g++) it gives the me following errors:
In file included from ShapeFactory.h:6:0,
from program.cpp:6:
Circle.h:15:51: error: cannot declare member function 'Shape::translate' within
'Circle'
Circle.h:18:32: error: cannot declare member function 'Shape::xreflect' within '
Circle'
Circle.h:21:32: error: cannot declare member function 'Shape::yreflect' within '
Circle'
Circle.h:25:53: error: cannot declare member function 'Shape::unTranslate' withi
n 'Circle'
Circle.h:28:34: error: cannot declare member function 'Shape::unXreflect' within
'Circle'
Circle.h:31:34: error: cannot declare member function 'Shape::unYreflect' within
'Circle'
In file included from ShapeFactory.h:7:0,
from program.cpp:6:
Triangle.h:15:51: error: cannot declare member function 'Shape::translate' withi
n 'Triangle'
Triangle.h:18:32: error: cannot declare member function 'Shape::xreflect' within
'Triangle'
Triangle.h:21:32: error: cannot declare member function 'Shape::yreflect' within
'Triangle'
Triangle.h:25:53: error: cannot declare member function 'Shape::unTranslate' wit
hin 'Triangle'
Triangle.h:28:34: error: cannot declare member function 'Shape::unXreflect' with
in 'Triangle'
Triangle.h:31:34: error: cannot declare member function 'Shape::unYreflect' with
in 'Triangle'
In file included from program.cpp:6:0:
ShapeFactory.h: In member function 'Shape* ShapeFactory::create()':
ShapeFactory.h:17:25: error: cannot allocate an object of abstract type 'Circle'
Circle.h:8:28: note: because the following virtual functions are pure within '
Circle':
Shape.h:15:16: note: virtual void Shape::translate(const Point&)
Shape.h:18:16: note: virtual void Shape::xreflect()
Shape.h:21:16: note: virtual void Shape::yreflect()
Shape.h:25:16: note: virtual void Shape::unTranslate(const Point&)
Shape.h:28:16: note: virtual void Shape::unXreflect()
Shape.h:31:16: note: virtual void Shape::unYreflect()
ShapeFactory.h:19:27: error: cannot allocate an object of abstract type 'Triangl
e'
Triangle.h:8:30: note: because the following virtual functions are pure within
'Triangle':
Shape.h:15:16: note: virtual void Shape::translate(const Point&)
Shape.h:18:16: note: virtual void Shape::xreflect()
Shape.h:21:16: note: virtual void Shape::yreflect()
Shape.h:25:16: note: virtual void Shape::unTranslate(const Point&)
Shape.h:28:16: note: virtual void Shape::unXreflect()
Shape.h:31:16: note: virtual void Shape::unYreflect()
I simply cannot figure out what is wrong, can anybody help me please? It pisses me off when something works in a compiler but doesn't in a different one.