Quick Q - using cstlib 'list' with types that contain virtual functions

This is a discussion on Quick Q - using cstlib 'list' with types that contain virtual functions within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to instantiate a list using the stl and I want to instantiate the list as pointers of ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    29

    Quick Q - using cstlib 'list' with types that contain virtual functions

    Hi, I'm trying to instantiate a list using the stl and I want to instantiate the list as pointers of a parent class Transport. Transport however has abstract virtual functions and my compiler is giving me an error because of it. This is the first time I've been allowed to use the stl containers so im not sure if im using it incorrectly or if something is actually wrong with my class. Below is Transport.h and my line of code trying to make the list. The virtual functions in Transport are also declared as virtual in the children of transport and are actually implemented in the grandchildren.

    Tranport.h
    Code:
    #ifndef TRANSPORT_H_
    #define TRANSPORT_H_
    
    #include <iostream>
    
    class Transport{
          public:
                 Transport(){}
                 double get_cost() const {return cost;}
                 void set_cost(double c) {cost = c;}
                              
                 virtual void calc_cost() = 0;
                 virtual void output(std::ostream & outs) = 0;
                 virtual void input(std::istream & ins) = 0;
          
          private:
                double cost;            
                  
    };
    #endif
    in my main i use this line:
    Code:
    list<Transport> list1;
    here is the error:

    99 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_list.h cannot declare field `std::_List_node<Transport>::_M_data' to be of type `Transport'

    99 C:\Dev-Cpp\include\c++\3.4.2\bits\stl_list.h because the following virtual functions are abstract:

    14 C:\Dev-Cpp\240c2\transport.h virtual void Transport::input(std::istream&)
    13 C:\Dev-Cpp\240c2\transport.h virtual void Transport:utput(std:stream&)
    12 C:\Dev-Cpp\240c2\transport.h virtual void Transport::calc_cost()

    thanks

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    A list<Transport> can only hold Transport objects. Since Transport is abstract the compiler doesn't let you declare one.
    You have to use list<Transport*> instead.
    Kurt

  3. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    you will need a virtual destructor too
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. overhead of virtual functions
    By coletek in forum C++ Programming
    Replies: 4
    Last Post: 01-12-2009, 11:56 AM
  2. Need help sorting a linked list. Beginner
    By scarlet00014 in forum C Programming
    Replies: 1
    Last Post: 09-27-2008, 06:16 PM
  3. deleting a node in linked list
    By BoneXXX in forum C Programming
    Replies: 18
    Last Post: 12-17-2007, 11:30 AM
  4. virtual functions
    By BKurosawa in forum C++ Programming
    Replies: 9
    Last Post: 08-12-2007, 02:12 AM
  5. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 10:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21