Why do I get C2259: 'Derived': cannot instantiate abstract class from
the following scenario?

class Abstract
{
public:
virtual int GetType() = 0;
virtual int GetApp() = 0;
virtual void GetVersion() = 0;
...
}

class Derived : public Abstract
{
public:
int GetType();
int GetApp();
void GetVersion(LPTSTR versio, size_t versionSize);
...
}

In class 'Derived' I've written an implementation for all three pure
virtual methods. Still the compiler claims the class 'Derived' is
abstract if I try to instantiate it???

Re: Cannot instantiate abstract class by Keith

Keith
Mon Jun 06 05:58:41 CDT 2005

GetVersion has different parameters.

"Rubio" <jtnim@hotmail.com> wrote in message
news:edf5a05f.0506060246.10ca7aca@posting.google.com...
> Why do I get C2259: 'Derived': cannot instantiate abstract class from
> the following scenario?
>
> class Abstract
> {
> public:
> virtual int GetType() = 0;
> virtual int GetApp() = 0;
> virtual void GetVersion() = 0;
> ...
> }
>
> class Derived : public Abstract
> {
> public:
> int GetType();
> int GetApp();
> void GetVersion(LPTSTR versio, size_t versionSize);
> ...
> }
>
> In class 'Derived' I've written an implementation for all three pure
> virtual methods. Still the compiler claims the class 'Derived' is
> abstract if I try to instantiate it???



Re: Cannot instantiate abstract class by Igor

Igor
Mon Jun 06 07:04:05 CDT 2005

"Rubio" <jtnim@hotmail.com> wrote in message
news:edf5a05f.0506060246.10ca7aca@posting.google.com
> Why do I get C2259: 'Derived': cannot instantiate abstract class from
> the following scenario?
>
> class Abstract
> {
> public:
> virtual int GetType() = 0;
> virtual int GetApp() = 0;
> virtual void GetVersion() = 0;
> ...
> }
>
> class Derived : public Abstract
> {
> public:
> int GetType();
> int GetApp();
> void GetVersion(LPTSTR versio, size_t versionSize);
> ...
> }
>
> In class 'Derived' I've written an implementation for all three pure
> virtual methods.

No you have not. You have provided overrides of GetType and GetAttr, but
you simply overloaded GetVersion. GetVersion with two parameters is
distinct and separate from GetVersion with no parameters. To override a
method of the base class, you need to match its signature exactly. Read
about function overloading in your favorite C++ textbook.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925



Re: Cannot instantiate abstract class by jtnim

jtnim
Wed Jun 08 00:40:33 CDT 2005

"Igor Tandetnik" <itandetnik@mvps.org> wrote in message news:<OQHaV$oaFHA.3808@TK2MSFTNGP14.phx.gbl>...
> No you have not. You have provided overrides of GetType and GetAttr, but
> you simply overloaded GetVersion. GetVersion with two parameters is
> distinct and separate from GetVersion with no parameters. To override a
> method of the base class, you need to match its signature exactly. Read
> about function overloading in your favorite C++ textbook.

Actually I think Keith is right. I didn't get the compiler error after
I had changed the Abstract class definition to:

virtual void GetVersion(LPTSTR versio, size_t versionSize) = 0;

Remember, this is not a question of function overriding us such,
rather than providing an implementation for a pure virtual function.
The implementation for a pure virtual function on a derived class has
to have the same parameters as in the base class. Checked it from my
favorite C++ texbook (Stroustrup). Or maybe this is what you meant. A
case of late night myopia on my part. Thanks anyway.

Re: Cannot instantiate abstract class by Tom

Tom
Wed Jun 08 03:52:13 CDT 2005

Igor Tandetnik wrote:
> "Rubio" <jtnim@hotmail.com> wrote in message
> news:edf5a05f.0506060246.10ca7aca@posting.google.com
>
>>Why do I get C2259: 'Derived': cannot instantiate abstract class from
>>the following scenario?
>>
>>class Abstract
>>{
>>public:
>> virtual int GetType() = 0;
>> virtual int GetApp() = 0;
>> virtual void GetVersion() = 0;
>> ...
>>}
>>
>>class Derived : public Abstract
>>{
>>public:
>> int GetType();
>> int GetApp();
>> void GetVersion(LPTSTR versio, size_t versionSize);
>> ...
>>}
>>
>>In class 'Derived' I've written an implementation for all three pure
>>virtual methods.
>
>
> No you have not. You have provided overrides of GetType and GetAttr, but
> you simply overloaded GetVersion.

No overloading here - GetVersion in Derived has hidden GetVersion in
Abstract.

Tom

Re: Cannot instantiate abstract class by Tim

Tim
Wed Jun 08 21:51:12 CDT 2005

Tom Widmer <tom_usenet@hotmail.com> wrote:
>
>Igor Tandetnik wrote:
>> "Rubio" <jtnim@hotmail.com> wrote:
>>
>>>Why do I get C2259: 'Derived': cannot instantiate abstract class from
>>>the following scenario?
>>>
>>>class Abstract
>>>{
>>>public:
>>> virtual int GetType() = 0;
>>> virtual int GetApp() = 0;
>>> virtual void GetVersion() = 0;
>>> ...
>>>}
>>>
>>>class Derived : public Abstract
>>>{
>>>public:
>>> int GetType();
>>> int GetApp();
>>> void GetVersion(LPTSTR versio, size_t versionSize);
>>> ...
>>>}
>>>
>>>In class 'Derived' I've written an implementation for all three pure
>>>virtual methods.
>>
>>
>> No you have not. You have provided overrides of GetType and GetAttr, but
>> you simply overloaded GetVersion.
>
>No overloading here - GetVersion in Derived has hidden GetVersion in
>Abstract.

Perhaps you could explain what you mean, because I think you are wrong.
The signature of Derived::GetVersion is different from
Abstract::GetVersion. Both Derived::GetVersion and Abstract::GetVersion
are visible in Derived, which is what caused the problem in the first
place.

Defining a new method with the same name but a different signature is
exactly the way I would define "overloading".
--
- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc

Re: Cannot instantiate abstract class by Victor

Victor
Wed Jun 08 22:06:27 CDT 2005

Tim Roberts wrote:
> Tom Widmer <tom_usenet@hotmail.com> wrote:
>>
>> Igor Tandetnik wrote:
>>> "Rubio" <jtnim@hotmail.com> wrote:
>>>
>>>> Why do I get C2259: 'Derived': cannot instantiate abstract class
>>>> from the following scenario?
>>>>
>>>> class Abstract
>>>> {
>>>> public:
>>>> virtual int GetType() = 0;
>>>> virtual int GetApp() = 0;
>>>> virtual void GetVersion() = 0;
>>>> ...
>>>> }
>>>>
>>>> class Derived : public Abstract
>>>> {
>>>> public:
>>>> int GetType();
>>>> int GetApp();
>>>> void GetVersion(LPTSTR versio, size_t versionSize);
>>>> ...
>>>> }
>>>>
>>>> In class 'Derived' I've written an implementation for all three
>>>> pure virtual methods.
>>>
>>>
>>> No you have not. You have provided overrides of GetType and
>>> GetAttr, but you simply overloaded GetVersion.
>>
>> No overloading here - GetVersion in Derived has hidden GetVersion in
>> Abstract.
>
> Perhaps you could explain what you mean, because I think you are
> wrong.

You think wrong.

> The signature of Derived::GetVersion is different from
> Abstract::GetVersion. Both Derived::GetVersion and
> Abstract::GetVersion are visible in Derived,

Actually, no. Read about name hiding in C++.

> which is what caused the
> problem in the first place.

No. The problem is that 'GetVersion' has no final overrider.

> Defining a new method with the same name but a different signature is
> exactly the way I would define "overloading".

Too bad.

Overloading works _only_ in the same scope. As soon as nested scopes
are involved, _hiding_ is taking over. You need to read the archives on
the difference between "overloading", "hiding", and "overriding". Keep
in mind that the derived class' scope is considered _nested_ in the base
class' scope.

V