I am having trouble with some code.
'Bar' : cannot instantiate abstract class
I have (finally) been able to recreate the error in a small amount of code.
struct SomeStruct
{
// ******
};
template <typename TIN, typename TOUT, typename TINDEX>
struct IFoo
{
public:
virtual void add(const TIN item) = 0; // <-- BAD
//virtual void add(const TOUT& item) = 0; // <-- GOOD
// ******
};
template <typename TVALUE, typename TINDEX>
struct Bar : IFoo<TVALUE &, TVALUE, TINDEX>
{
public:
void add(const TVALUE& item)
{
// ******
}
// ******
};
int main(int argc, char *argv[])
{
SomeStruct someStruct;
Bar<SomeStruct, int> bar = Bar<SomeStruct, int>();
bar.add(someStruct);
// ******
}
Can anyone please advise WHY using a reference with the template parameter is causing this?
The issue here is that when you write
const TIN
andTIN
is a reference type, theconst
applies to the reference and not to the value type.This is why you are seeing different behaviour for
const TIN
andconst TOUT&
even when you think they should be the same.A simple fix for this is to add the
const
to the value type in yourIFoo
instantiation: