468,309 Members | 1,140 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,309 developers. It's quick & easy.

error: "Cannot create a value of the abstract class base"

class base
{
public:
base()
{

}
base(int number)
{
priNumber = number ;
}

void pubFunc1()
{
// use 'priNumber' and do some tasks
}

void pubFunc2()
{
// use 'priNumber' and do some tasks
}

virtual void pubFunc3() = 0 ;

private:
int priNumber ;
} ;

class derived_one : public base
{
public:
derived_one()
{
base( 1 ) ;
}

void pubFunc3()
{
//
}

} ;

class derived_two : public base
{
public:
derived_two()
{
base( 2 ) ;
}

void pubFunc3()
{
//
}
};

I'm getting the compilation error: "Cannot create a value of the
abstract class base".

Only the objects of the derived classes make sense. Also, there is a
common implementation required for the pubFunc1 & pubFunc2 based on
the parameter 'priNumber' value. What is the other way of passing this
parameter to the base class function?

Jul 5 '07 #1
2 1835
On Jul 5, 1:51 pm, "qazmlp1...@rediffmail.com"
<qazmlp1...@rediffmail.comwrote:
class base
{
public:
base()
{

}
base(int number)
{
priNumber = number ;
}

void pubFunc1()
{
// use 'priNumber' and do some tasks
}

void pubFunc2()
{
// use 'priNumber' and do some tasks
}

virtual void pubFunc3() = 0 ;

private:
int priNumber ;

} ;

class derived_one : public base
{
public:
derived_one()
{
base( 1 ) ;
}
Here, you are *creating* a new object of base class. That is not
allowed since base class is abstract.

But I guess what you want is not a *new* object of base class, but
initializing the base component of derived_one using the appropriate
value. Use member initialization list.

derived_one() : base(1) { }

>
void pubFunc3()
{
//
}

} ;

class derived_two : public base
{
public:
derived_two()
{
base( 2 ) ;
}
Same. Use
derived_two() : base(2) { }
-Neelesh

Jul 5 '07 #2

qa********@rediffmail.com <qa********@rediffmail.comwrote
in <11*********************@e16g2000pri.googlegroups. com>:
class base
{
public:
base()
{

}
virtual ~base () {}
base(int number)
{
priNumber = number ;
}
[...]
virtual void pubFunc3() = 0 ;

private:
int priNumber ;
} ;

class derived_one : public base
{
public:
derived_one()
{
base( 1 ) ;
}
[...]
I'm getting the compilation error: "Cannot create a value
of the abstract class base".
Try:

derived_one () : base (1)
{
}

--
Pavel Lepin
Jul 5 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Andreas Poller | last post: by
1 post views Thread by marknewbery | last post: by
5 posts views Thread by crystalattice | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.