Hi all.
I'm trying to create a pure abstract numeric class so that I can have an implementation of different number systems but they all are derived from the base class. So I have
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
class numberBase
{
public:
virtual void assign(unsigned int numerator, unsigned int denominator) = 0;
// Assignment Operators
virtual numberBase & operator=(const numberBase& other) = 0;
virtual numberBase & operator+=(const numberBase& other) = 0;
virtual numberBase & operator-=(const numberBase& other) = 0;
virtual numberBase & operator*=(const numberBase& other) = 0;
//Arithmetic operators.
virtual numberBase operator+(const numberBase& other) = 0;
virtual numberBase operator-(const numberBase& other) = 0;
virtual numberBase operator*(const numberBase& other) = 0;
// Conditional Operators.
virtual bool operator==(const numberBase& other) = 0;
virtual bool operator!=(const numberBase& other) = 0;
virtual bool operator>(const numberBase& other) = 0;
virtual bool operator<(const numberBase& other) = 0;
virtual bool operator>=(const numberBase& other) = 0;
virtual bool operator<=(const numberBase& other) = 0;
};
|
but when I try to compile this with a derived class, I get
In file included from numbers/probFP.h:2,
from numbers/probFP.cpp:1:
numbers/numberBase.h:16: error: invalid abstract return type for member function 'virtual numberBase numberBase::operator+(const numberBase&)'
numbers/numberBase.h:6: note: because the following virtual functions are pure within 'numberBase':
numbers/numberBase.h:8: note: virtual void numberBase::assign(unsigned int, unsigned int)
numbers/numberBase.h:10: note: virtual numberBase& numberBase::operator=(const numberBase&)
numbers/numberBase.h:11: note: virtual numberBase& numberBase::operator+=(const numberBase&)
numbers/numberBase.h:12: note: virtual numberBase& numberBase::operator-=(const numberBase&)
numbers/numberBase.h:13: note: virtual numberBase& numberBase::operator*=(const numberBase&)
numbers/numberBase.h:16: note: virtual numberBase numberBase::operator+(const numberBase&)
numbers/numberBase.h:17: note: virtual numberBase numberBase::operator-(const numberBase&)
numbers/numberBase.h:18: note: virtual numberBase numberBase::operator*(const numberBase&)
numbers/numberBase.h:21: note: virtual bool numberBase::operator==(const numberBase&)
numbers/numberBase.h:22: note: virtual bool numberBase::operator!=(const numberBase&)
numbers/numberBase.h:23: note: virtual bool numberBase::operator>(const numberBase&)
numbers/numberBase.h:24: note: virtual bool numberBase::operator<(const numberBase&)
numbers/numberBase.h:25: note: virtual bool numberBase::operator>=(const numberBase&)
numbers/numberBase.h:26: note: virtual bool numberBase::operator<=(const numberBase&)
numbers/numberBase.h:17: error: invalid abstract return type for member function 'virtual numberBase numberBase::operator-(const numberBase&)'
numbers/numberBase.h:6: note: since type 'numberBase' has pure virtual functions
numbers/numberBase.h:18: error: invalid abstract return type for member function 'virtual numberBase numberBase::operator*(const numberBase&)'
numbers/numberBase.h:6: note: since type 'numberBase' has pure virtual functions
In file included from numbers/probFP.cpp:1:
numbers/probFP.h:19: error: invalid abstract return type for member function 'probFP probFP::operator+(const probFP&)'
numbers/probFP.h:7: note: because the following virtual functions are pure within 'probFP':
numbers/numberBase.h:10: note: virtual numberBase& numberBase::operator=(const numberBase&)
numbers/numberBase.h:11: note: virtual numberBase& numberBase::operator+=(const numberBase&)
numbers/numberBase.h:12: note: virtual numberBase& numberBase::operator-=(const numberBase&)
numbers/numberBase.h:13: note: virtual numberBase& numberBase::operator*=(const numberBase&)
numbers/numberBase.h:16: note: virtual numberBase numberBase::operator+(const numberBase&)
numbers/numberBase.h:17: note: virtual numberBase numberBase::operator-(const numberBase&)
numbers/numberBase.h:18: note: virtual numberBase numberBase::operator*(const numberBase&)
numbers/numberBase.h:21: note: virtual bool numberBase::operator==(const numberBase&)
numbers/numberBase.h:22: note: virtual bool numberBase::operator!=(const numberBase&)
numbers/numberBase.h:23: note: virtual bool numberBase::operator>(const numberBase&)
numbers/numberBase.h:24: note: virtual bool numberBase::operator<(const numberBase&)
numbers/numberBase.h:25: note: virtual bool numberBase::operator>=(const numberBase&)
numbers/numberBase.h:26: note: virtual bool numberBase::operator<=(const numberBase&)
numbers/probFP.h:20: error: invalid abstract return type for member function 'probFP probFP::operator-(const probFP&)'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.h:21: error: invalid abstract return type for member function 'probFP probFP::operator*(const probFP&)'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp: In member function 'probFP probFP::operator+(const probFP&)':
numbers/probFP.cpp:93: error: invalid abstract return type for member function 'probFP probFP::operator+(const probFP&)'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:95: error: cannot allocate an object of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:95: error: cannot declare variable 'result' to be of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:97: error: cannot allocate an object of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp: In member function 'probFP probFP::operator-(const probFP&)':
numbers/probFP.cpp:100: error: invalid abstract return type for member function 'probFP probFP::operator-(const probFP&)'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:102: error: cannot allocate an object of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:102: error: cannot declare variable 'result' to be of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:104: error: cannot allocate an object of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp: In member function 'probFP probFP::operator*(const probFP&)':
numbers/probFP.cpp:107: error: invalid abstract return type for member function 'probFP probFP::operator*(const probFP&)'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:109: error: cannot allocate an object of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:109: error: cannot declare variable 'result' to be of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
numbers/probFP.cpp:111: error: cannot allocate an object of abstract type 'probFP'
numbers/probFP.h:7: note: since type 'probFP' has pure virtual functions
... etc,
So a numeric operator in C++ needs a reference to an objcet to be returned to allow chaining, but it can't do that as you can't create an instance of a pure abstract class (which will actually never be created). Is there a nicer way around this? Or should I just not use Pure virtual functions?