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

Q

qazmlp1209

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

Neelesh Bodas

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
 
P

Pavel Lepin

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)
{
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top