Mam projekt dla szkoły w C++ i utknąłem na jednej części: Muszę przeciążyć operatorów + i * do pracy z figurami geometrycznymi. Nie stanowiło to problemu, ale tutaj, gdzie to nie działa: muszę zadeklarować operatora jako czystą metodę wirtualną, w klasie abstrakcyjnej, z której wywodzą się wszystkie inne klasy.Czysty wirtualny operator
#include<iostream>
using namespace std;
class Figabs {
protected:
int fel;
public:
int getFEL() { return fel; }
virtual Figabs operator +()=0; /*this is where I get an error: function returning abstract class “Figabs” is not allowed : function Figabs::operator+ is a pure virtual function */
};
class Coord {
public:
int cx, cy;
public:
Coord(){
cx = cy = 0;
}
Coord (const int x, const int y) {
cx = x;
cy = y;
}
Coord (const Coord &din) {
cx = din.cx;
cy = din.cy;
}
~Coord() { }
void setX(const int val) { cx = val; } ;
void setY(const int val) { cy = val; };
int getX() { return cx; }
int getY() { return cy; }
};
class Point : public Coord, public Figabs { //one of the figures
public:
Point() {
setX(0);
setY(0);
fel = 0;
}
Point(const int x, const int y): Coord (x,y) {
fel = 0;
}
Point(const Point &din): Coord (din) {
fel = din.fel;
}
~Point() { }
Point operator +(const Coord &vector) { /*this works perfectly when I delete the declaration from the abstract class Figabs, but I don’t know how to make them work together */
int xp = cx + vector.cx;
int yp = cy + vector.cy;
return (Point (xp, yp));
}
Point operator *(const Coord &vector) {
Point temp;
temp.cx = cx * vector.cx;
temp.cy = cy * vector.cy;
return (temp);
}
};
Dziękuję i proszę o cierpliwość ze mną, jest to mój pierwszy kontakt z C++.
Czy zamiast tego próbowałeś umieścić deklarację w pliku .H? – Shark
@Shark: co dokładnie by to zmieniło? – Mat
wirtualny operator Figabs +() = 0 bez argumentów -> Operator punktu + (const Coord i wektor) z innym typem zwróconym? ich podpis musi być taki sam ... – Exceptyon