Thread: Pure virtual method

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    11

    Pure virtual method

    Hello


    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 "Member::sale" is a pure virtual function
    Member is class base.
    - Teacher (derived)
    - Student (derived)
    I'm creating a new object to trying find, but how the method is abstract i can't do that.


    What's the suggestion to solve my problem?
    Thanks!

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You are trying to make objects of the base class.
    When there is a (pure) virtual function, objects of derived classes make sense.
    The base class is only there as some sort of common interface.

    Btw, next time, post your code too.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    member is a collection of members
    Code:
    Member *Sop::findMember(string bi){
      Member *m = new Member(bi, NULL); 
      return member.find(m)
    }
    I want find member, but first i need create..How can i do that without create object?
    Thanks!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,267
    Since Member has pure virtual functions, you must create instances of the derived classes.
    Code:
    Member *Sop::findMember(string bi){
      Member *m = new Student(bi, NULL); // Note that we are now creating a Student instead of a Member
      return member.find(m)
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    Quote Originally Posted by bithub View Post
    Since Member has pure virtual functions, you must create instances of the derived classes.
    Code:
    Member *Sop::findMember(string bi){
      Member *m = new Student(bi, NULL); // Note that we are now creating a Student instead of a Member
      return member.find(m)
    }
    Thanks, problem solved.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,727
    Quote Originally Posted by JacksonM
    member is a collection of members
    You might want to rename it members or maybe member_collection. It would be better if you actually showed the definition of this object: many things in C++ depends on type, and "a collection of members" does not tell us much about the type.

    Quote Originally Posted by bithub
    Code:
    Member *Sop::findMember(string bi){
      Member *m = new Student(bi, NULL); // Note that we are now creating a Student instead of a Member
      return member.find(m)
    }
    It might be feasible to just write:
    Code:
    Member* Sop::findMember(string bi) {
        Student student(bi, NULL);
        return member.find(&student);
    }
    Otherwise say, std::unique pointer should be used, or at the very least one should manually have a delete to match the new.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  2. Pure Virtual Function - Virtual Method Table
    By u_peerless in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2008, 01:19 AM
  3. Need help on pure virtual method
    By wanex in forum C++ Programming
    Replies: 9
    Last Post: 03-25-2006, 07:51 AM
  4. Pure virtual implementation, or not.
    By Eibro in forum C++ Programming
    Replies: 2
    Last Post: 03-19-2003, 08:05 PM
  5. Virtual & Pure virtual destructors
    By BMJ in forum C++ Programming
    Replies: 61
    Last Post: 08-22-2002, 09:38 AM