dereived class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mar11
    New Member
    • Sep 2009
    • 38

    dereived class

    Hi,

    I want to derive a class from the main class as shown in the code below. But I am not sure how can I deal with the constructors of both classes.


    Code:
    // erasing from map
    #include <iostream>
    #include <string>
    #include <queue>
    #include <stdio.h>
    #include <sstream>
    using namespace std;
    
    
    class ma
    {
      ma(string st);
    };
    
    class sub
     :public ma
    {
    public:
     sub(string a);
    };
    
    int main ()
    {
    
    std::string st = "hello";
    queue<sub*> test;
    
    test.push(new sub(st));
    
      return 0;
    }
    If I compile the this code I got this error message:
    /tmp/ccQvbxR8.o: In function `main':

    test.cpp:(.text +0xf6): undefined reference to `sub::sub(std:: basic_string<ch ar, std::char_trait s<char>, std::allocator< char> >)'
    collect2: ld returned 1 exit status

    May someone please help!!

    Regards
  • MartijnHoekstra
    New Member
    • Sep 2010
    • 39

    #2
    you should implement (declare) the constructors.
    Now they are just definitions.

    the sub could look like:

    Code:
    class sub :public ma 
    { 
    public: 
     sub(string a) : ma(a) {} 
    };

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      you should implement (declare) the constructors.
      Now they are just definitions.
      When you implement a function, that is when you code it, that is a definition.

      When you code a function prototype, that is a declaration.

      Comment

      Working...