For Programmers: Free Programming Magazines  


Home > Archive > VC Language > June 2005 > Cannot instantiate abstract class









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Cannot instantiate abstract class
Rubio

2005-06-06, 8:59 am

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???
Keith MacDonald

2005-06-06, 8:59 am

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???



Igor Tandetnik

2005-06-06, 4:02 pm

"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


Rubio

2005-06-08, 4:02 am

"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.
Tom Widmer

2005-06-08, 9:00 am

Igor Tandetnik wrote:
> "Rubio" <jtnim@hotmail.com> wrote in message
> news:edf5a05f.0506060246.10ca7aca@posting.google.com
>
>
>
> 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
Tim Roberts

2005-06-09, 3:59 am

Tom Widmer <tom_usenet@hotmail.com> wrote:
>
>Igor Tandetnik wrote:
>
>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
Victor Bazarov

2005-06-09, 3:59 am

Tim Roberts wrote:
> Tom Widmer <tom_usenet@hotmail.com> wrote:
>
> 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


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com