A problem with inheritance

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • axel22@gmail.com

    #1

    A problem with inheritance

    Hello again and thnx to everybody for previous feedbacks.
    This code works quite nicely when I omit the inheritance in
    MyContainer.h

    ---------------
    MyClass.h
    ---------------

    #pragma once

    #include "MyContaine r.h"

    class MyClass {
    private:
    int myInteger;
    MyContainer memberContainer ;
    public:
    MyClass(void);
    ~MyClass(void);

    };

    ---------------------
    MyContainer.h
    ---------------------

    #pragma once

    #include <vector>

    using namespace std;

    class MyClass; // forward declaration

    class MyContainer : BaseContainer<M yClass *{
    private:
    vector<MyClass *myVector;
    MyClass *pointer;
    public:
    MyContainer(voi d);
    int add(const MyClass *element) { return 1; }
    ~MyContainer(vo id);

    };

    ------------------------
    BaseContainer.h
    ------------------------

    #pragma once

    template <class Tip>
    class BaseContainer
    {
    public:
    BaseContainer(v oid);
    virtual int add(const Tip element) = 0;
    ~BaseContainer( void);
    };


    -------------
    main.cpp
    -------------

    #include <iostream>

    #include "MyClass.h"
    #include "MyContaine r.h"

    void main() {
    MyClass myInstance;
    MyContainer myContainerInst ance;

    }



    However, with BaseContainer inherited to MyContainer, the following
    problem occurs:

    f:\C++\Projects \Probarka\MyCla ss.h(9) : error C2259: 'MyContainer' :
    cannot instantiate abstract class
    due to following members:
    'int BaseContainer<T ip>::add(const Tip)' : pure virtual
    function was not defined
    with
    [
    Tip=MyClass *
    ]
    f:\C++\Projects \Probarka\BaseC ontainer.h(8) : see declaration
    of 'BaseContainer< Tip>::add'
    with
    [
    Tip=MyClass *
    ]


    I can't understand what's wrong. Without the inheritance it works
    perfectly. Please help.

  • Ron Natalie

    #2
    Re: A problem with inheritance

    axel22@gmail.co m wrote:
    int add(const MyClass *element) { return 1; }
    does not have override
    virtual int add(const Tip element) = 0;
    when Tip is MyClass*.

    If Tip is Myclass*, then const Tip is:

    MyClass* const

    hence the overrider would be
    int add(MyClass* const element) { ..

    Comment

    • axel22@gmail.com

      #3
      Re: A problem with inheritance


      Ron Natalie je napisao/la:
      axel22@gmail.co m wrote:
      >
      int add(const MyClass *element) { return 1; }
      >
      does not have override
      >
      virtual int add(const Tip element) = 0;
      >
      when Tip is MyClass*.
      >
      If Tip is Myclass*, then const Tip is:
      >
      MyClass* const
      >
      hence the overrider would be
      int add(MyClass* const element) { ..
      I tried changing the 'add' method to what you've said, but I'm still
      getting the same error message.

      Comment

      • Thomas Tutone

        #4
        Re: A problem with inheritance

        axel22@gmail.co m wrote:
        Hello again and thnx to everybody for previous feedbacks.
        This code works quite nicely when I omit the inheritance in
        MyContainer.h
        >
        ---------------
        MyClass.h
        [snip]
        MyContainer.h
        ---------------------
        >
        #pragma once
        >
        #include <vector>
        >
        using namespace std;
        >
        class MyClass; // forward declaration
        >
        class MyContainer : BaseContainer<M yClass *{
        private:
        vector<MyClass *myVector;
        MyClass *pointer;
        public:
        MyContainer(voi d);
        int add(const MyClass *element) { return 1; }
        ~MyContainer(vo id);
        >
        };
        >
        [snip]
        -------------
        main.cpp
        -------------
        >
        #include <iostream>
        >
        #include "MyClass.h"
        #include "MyContaine r.h"
        >
        void main() {
        MyClass myInstance;
        MyContainer myContainerInst ance;
        >
        }
        >
        [snip]

        Ron Natalie has already answered your question. Two comments on your
        code, though.

        First, please don't put "#include namespace std" in a header file.
        It's a bad habit and will come back to bite you - or one of your
        colleagues - some day.

        Second, don't use "void main" - it's not standard, and probably won't
        work if you switch to a different compiler.

        Best regards,

        Tom

        Comment

        • axel22@gmail.com

          #5
          Re: A problem with inheritance


          Thomas Tutone je napisao/la:
          axel22@gmail.co m wrote:
          >
          Hello again and thnx to everybody for previous feedbacks.
          This code works quite nicely when I omit the inheritance in
          MyContainer.h

          ---------------
          MyClass.h
          >
          [snip]
          >
          MyContainer.h
          ---------------------

          #pragma once

          #include <vector>

          using namespace std;

          class MyClass; // forward declaration

          class MyContainer : BaseContainer<M yClass *{
          private:
          vector<MyClass *myVector;
          MyClass *pointer;
          public:
          MyContainer(voi d);
          int add(const MyClass *element) { return 1; }
          ~MyContainer(vo id);

          };
          >
          [snip]
          >
          -------------
          main.cpp
          -------------

          #include <iostream>

          #include "MyClass.h"
          #include "MyContaine r.h"

          void main() {
          MyClass myInstance;
          MyContainer myContainerInst ance;

          }
          >
          [snip]
          >
          Ron Natalie has already answered your question. Two comments on your
          code, though.
          >
          First, please don't put "#include namespace std" in a header file.
          It's a bad habit and will come back to bite you - or one of your
          colleagues - some day.
          >
          Second, don't use "void main" - it's not standard, and probably won't
          work if you switch to a different compiler.
          >
          Best regards,
          >
          Tom

          Thnx Tom, you're right, I shouldn't use void main, but I usually use
          WinMain anyway. As for the using namespace directive, you're right,
          I'll remove it.
          If I understood correctly, Ron Natalie said that I should change
          int add(const MyClass *element) { return 1; };
          to:
          int add(MyClass const *element) { return 1; };

          I tried doing this, but with no effect.

          Comment

          • Thomas Tutone

            #6
            Re: A problem with inheritance


            axe...@gmail.co m wrote:
            Thnx Tom, you're right, I shouldn't use void main, but I usually use
            WinMain anyway. As for the using namespace directive, you're right,
            I'll remove it.
            If I understood correctly, Ron Natalie said that I should change
            int add(const MyClass *element) { return 1; };
            to:
            int add(MyClass const *element) { return 1; };
            No, he said change it to:

            int add(MyClass* const element) { return 1; }

            (Lose that ";" after the closing bracket of the function definition, by
            the way.)

            Best regards,

            Tom

            Comment

            • axel22@gmail.com

              #7
              Re: A problem with inheritance

              I'm getting a different error message, though...


              MyContainer.obj : error LNK2019: unresolved external symbol "public:
              __thiscall BaseContainer<c lass MyClass *>::~BaseContai ner<class MyClass
              *>(void)" (??1?$BaseConta iner@PAVMyClass @@@@QAE@XZ) referenced in
              function __unwindfunclet $??0MyContainer @@QAE@XZ$0
              MyContainer.obj : error LNK2019: unresolved external symbol "public:
              __thiscall BaseContainer<c lass MyClass *>::BaseContain er<class MyClass
              *>(void)" (??0?$BaseConta iner@PAVMyClass @@@@QAE@XZ) referenced in
              function "public: __thiscall MyContainer::My Container(void) "
              (??0MyContainer @@QAE@XZ)

              What does this mean? I provided definitions for the constructor and the
              destructor in BaseContainer.c pp

              Comment

              • axel22@gmail.com

                #8
                Re: A problem with inheritance

                ps Thomas: I changed it to what Ron said.

                Comment

                • axel22@gmail.com

                  #9
                  Re: A problem with inheritance

                  Strange, this reports the previous error message in the project I've
                  posted, but in my original project everything is working neatly.

                  Ron, Thomas - Thnx for your feedback

                  Bye

                  Comment

                  • Thomas Tutone

                    #10
                    Re: A problem with inheritance


                    axel22@gmail.co m wrote:
                    I'm getting a different error message, though...
                    >
                    >
                    MyContainer.obj : error LNK2019: unresolved external symbol "public:
                    __thiscall BaseContainer<c lass MyClass *>::~BaseContai ner<class MyClass
                    *>(void)" (??1?$BaseConta iner@PAVMyClass @@@@QAE@XZ) referenced in
                    function __unwindfunclet $??0MyContainer @@QAE@XZ$0
                    MyContainer.obj : error LNK2019: unresolved external symbol "public:
                    __thiscall BaseContainer<c lass MyClass *>::BaseContain er<class MyClass
                    *>(void)" (??0?$BaseConta iner@PAVMyClass @@@@QAE@XZ) referenced in
                    function "public: __thiscall MyContainer::My Container(void) "
                    (??0MyContainer @@QAE@XZ)
                    >
                    What does this mean?
                    It means you didn't provide definitions for the constructor and
                    destructor for template<class BaseContainer.
                    >I provided definitions for the constructor and the
                    destructor in BaseContainer.c pp
                    You didn't post BaseContainer.c pp, but the solution to your problem is
                    in the FAQ (sections 35.12 and following):



                    Best regards,

                    Tom

                    Comment

                    • axel22@gmail.com

                      #11
                      Re: A problem with inheritance


                      Thomas Tutone je napisao/la:
                      axel22@gmail.co m wrote:
                      I'm getting a different error message, though...


                      MyContainer.obj : error LNK2019: unresolved external symbol "public:
                      __thiscall BaseContainer<c lass MyClass *>::~BaseContai ner<class MyClass
                      *>(void)" (??1?$BaseConta iner@PAVMyClass @@@@QAE@XZ) referenced in
                      function __unwindfunclet $??0MyContainer @@QAE@XZ$0
                      MyContainer.obj : error LNK2019: unresolved external symbol "public:
                      __thiscall BaseContainer<c lass MyClass *>::BaseContain er<class MyClass
                      *>(void)" (??0?$BaseConta iner@PAVMyClass @@@@QAE@XZ) referenced in
                      function "public: __thiscall MyContainer::My Container(void) "
                      (??0MyContainer @@QAE@XZ)

                      What does this mean?
                      >
                      It means you didn't provide definitions for the constructor and
                      destructor for template<class BaseContainer.
                      >
                      I provided definitions for the constructor and the
                      destructor in BaseContainer.c pp
                      >
                      You didn't post BaseContainer.c pp, but the solution to your problem is
                      in the FAQ (sections 35.12 and following):
                      >

                      >
                      Best regards,
                      >
                      Tom

                      Thnx!

                      Comment

                      • Ron Natalie

                        #12
                        Re: A problem with inheritance

                        axel22@gmail.co m wrote:
                        \

                        right, I shouldn't use void main, but I usually use
                        WinMain anyway. As for the using namespace directive, you're right,
                        I'll remove it.
                        If I understood correctly, Ron Natalie said that I should change
                        int add(const MyClass *element) { return 1; };
                        to:
                        int add(MyClass const *element) { return 1; };
                        >
                        I tried doing this, but with no effect.
                        >
                        No you don't understand me. The above two are IDENTICAL
                        and NOT what I wrote.

                        int addMyclass * const element)
                        is what I wrote and I meant it.

                        Comment

                        • Jerry Coffin

                          #13
                          Re: A problem with inheritance

                          In article <44d89b50$0$241 70$9a6e19ea@new s.newshosting.c om>,
                          ron@spamcop.net says...

                          [ ... ]
                          int addMyclass * const element)
                          is what I wrote and I meant it.
                          Well, I'm pretty sure what you meant (and wrote) was:

                          int add(Myclass * const element)

                          but what's a missing parenthesis between friends?


                          [I suppose the answer to that would depend on whether you classify the
                          compiler as a friend. :-) ]

                          --
                          Later,
                          Jerry.

                          The universe is a figment of its own imagination.

                          Comment

                          • axel22@gmail.com

                            #14
                            Re: A problem with inheritance


                            Ron Natalie je napisao/la:
                            axel22@gmail.co m wrote:
                            \
                            >
                            right, I shouldn't use void main, but I usually use
                            WinMain anyway. As for the using namespace directive, you're right,
                            I'll remove it.
                            If I understood correctly, Ron Natalie said that I should change
                            int add(const MyClass *element) { return 1; };
                            to:
                            int add(MyClass const *element) { return 1; };

                            I tried doing this, but with no effect.
                            No you don't understand me. The above two are IDENTICAL
                            and NOT what I wrote.
                            >
                            int addMyclass * const element)
                            is what I wrote and I meant it.
                            Thnx, got it. It works now.

                            Comment

                            • Markus Schoder

                              #15
                              Re: A problem with inheritance

                              Thomas Tutone wrote:
                              axel22@gmail.co m wrote:
                              >
                              >Hello again and thnx to everybody for previous feedbacks.
                              >This code works quite nicely when I omit the inheritance in
                              >MyContainer. h
                              >>
                              >---------------
                              >MyClass.h
                              >
                              [snip]
                              >
                              >MyContainer. h
                              >---------------------
                              >>
                              >#pragma once
                              >>
                              >#include <vector>
                              >>
                              >using namespace std;
                              >>
                              >class MyClass; // forward declaration
                              >>
                              >class MyContainer : BaseContainer<M yClass *{
                              >private:
                              > vector<MyClass *myVector;
                              > MyClass *pointer;
                              >public:
                              > MyContainer(voi d);
                              > int add(const MyClass *element) { return 1; }
                              > ~MyContainer(vo id);
                              >>
                              >};
                              >>
                              >
                              [snip]
                              >
                              >-------------
                              >main.cpp
                              >-------------
                              >>
                              >#include <iostream>
                              >>
                              >#include "MyClass.h"
                              >#include "MyContaine r.h"
                              >>
                              >void main() {
                              > MyClass myInstance;
                              > MyContainer myContainerInst ance;
                              >>
                              >}
                              >>
                              >
                              [snip]
                              >
                              Ron Natalie has already answered your question. Two comments on your
                              code, though.
                              >
                              First, please don't put "#include namespace std" in a header file.
                              It's a bad habit and will come back to bite you - or one of your
                              colleagues - some day.
                              >
                              Second, don't use "void main" - it's not standard, and probably won't
                              work if you switch to a different compiler.
                              Third, '#pragma once' is also not standard and should be guarded by
                              #ifdef's identifying your specific compiler (other compilers will need
                              the usual include guards of course).

                              Comment

                              Working...