-2

I have problem using class base with pure virtual method. The method should be virtual.

I get this error:

Error: object of abstract class type "Membro" is not allowed: function "Membro::mensalidade" is a pure virtual function

enter image description here

Anyone can help?

Thanks!

Jean-Baptiste Yunès
  • 34,548
  • 4
  • 48
  • 69
  • 2
    You can't create an instance of an abstract type. Membro contains a pure virtual method and therefore is abstract. You cannot create an object of type Membro. (I know this is essentially what the error message said but ...) – Borgleader Dec 16 '14 at 15:59
  • 1
    `Membro` contains a pure virtual function so cannot be instantiated. – Bathsheba Dec 16 '14 at 15:59
  • What do you mean by saying "should be virtual"? – alediaferia Dec 16 '14 at 16:04

5 Answers5

2

You cannot instantiate an abstract class.

Any class is abstract if it contains at least one one pure-virtual method (e.g. anything with =0; at the end of the function signature).

You must instantiate a class derived from the abstract class (which must implement the pure-virtual function(s) to not be abstract itself) instead and return a pointer to that. The pointer's type can be the type of the abstract class, and the object that it points to must be a subclass of the abstract class, allowing you to access the derived classes polymorphically.

John Humphreys
  • 37,047
  • 37
  • 155
  • 255
2

Just like the error says Membro::mensalidade is a pure virtual function. This means you cannot use Membro directly but you have to implement it in a subclass and reimplement at least all the pure virtual methods.

alediaferia
  • 2,537
  • 19
  • 22
  • But i want use attributes of member to findmember. How the method mensalidade is pure and i can't create a new object. How can i do this? – Mister Jonh Dec 16 '14 at 16:22
2

If a class contains at least one pure virtual function, then that class is abstract. That means you cannot create object of that class. Abstract classes serves as interface classes to more derived class.

However, you can define that pure virtual function. But this feature is of limited use except to impress your fellow workers.

ravi
  • 10,994
  • 1
  • 18
  • 36
2

The problem is clear from the error message: you may not instantiate an object of an abstract class.

However it seems that there is no need to create an object of the abstract class in the function you showed. As I have understood the function searches an object with the given string bi. You could use a lambda expression in some search method.

That is the problem is that the design of the function is incorrect. To get more exact answer you should show the function and what is the type of membros.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

You have to imeplement the virtual method in a derived class. It is pure virtual to make sure that you implement it when deriving from the class. It also means that there is no default implementaion of that method.

This kind of class has a name they are called Abstract Classes. or Interfaces.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97