I have “Cannot allocate an object of abstract type” error.
I get it when I create an object Be
(inheriting from abstract class Ae
) when passing it to a method taking const Ae&
. I thought it shouldn't be a problem when passing as a reference. The error says: error: allocating an object of abstract class type Be
.
The logic of a program goes as follows:
//A.hpp
struct A : public std::exception { //Exception class
class Ae { //Error message class
virtual const char* message() const = 0;
inline operator const char*() const {
return message();
}
};
// fields describing where the error occurred in file input
int a, b, c, d;
// string reporting what happened
const char* msg;
A( const Ae& e, int a, int b, int c, int d ) : a(a), b(b), c(c), d(d), msg(e)
{}
const char* what() const throw() { /* printing some nice report */ }
~A() throw () {}
};
//B.hpp
struct B : public A {
B( const Ae& e, int a, int b, int c, int d ) : A( e,a,b,c,d )
{}
struct Be : public Ae { //particular error having relevant class name e.g. IncorrectInput
const char* message() const {
return "Some error description";
}
}; // more of alikes
};
//AUser.hpp
struct AUser { // some module that will be throwing A
int a, b, c, d;
A Exception( const Ae& e ) {
return A( e, a, b, c, d );
}
};
//BUser.hpp
struct BUser : public AUser {
void fun() { // any method
// (...) does sth
if ( goeswrong )
throw Exception( B::Be() );
}
};
What is wrong with my code?
To clarify the goal of such structures - AUser is CsvFile, BUser is ParticularCsvFile (with methods recognizing which field contains which datatype), A is CsvFileExceotion, B is ParticularFileException, Ae is ErrorCode, Be is for example ErrorWhenReadingPricesCell
The error pops out for line containing throw Exception( B::Be() );
claming additionally that note: unimplemented pure virtual method 'message' in 'Be'
public: virtual const char* message() const = 0;
Edit
As requested, the exact g++ -Wall -pedantic -Wextra goes below.
Please note that I simplified the code above and changed names:
BUser == DividendsFile, B == DividendsFileReadingException, Be == UnexpectedStockDividend, AUser == CsvFile, A == CsvFileReadingException, Be == ErrorCode
quotreader2.cpp:18:5: warning: unused parameter ‘argc’ [-Wunused-parameter]
Input/DividendsFile.cpp: In member function ‘int DividendsFile::getStockDividend()’:
Input/DividendsFile.cpp:28:85: error: cannot allocate an object of abstract type ‘DividendsFileReadingException::UnexpectedStockDividend’
In file included from Input/DividendsFile.cpp:3:0:
Input/Exceptions/DividendsFileReadingException.hpp:28:12: note: because the following virtual functions are pure within ‘DividendsFileReadingException::UnexpectedStockDividend’:
In file included from Input/CsvFile.hpp:4:0,
from Input/DividendsFile.hpp:4,
from Input/DividendsFile.cpp:2:
Input/Exceptions/CsvFileReadingException.hpp:17:37: note: virtual const char* CsvFileReadingException::ErrorCode::message() const
make: *** [bin/test.bin] Error 1