Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C2244 and inheritance error

C2244 and inheritance error

Scheduled Pinned Locked Moved C / C++ / MFC
visual-studioalgorithmsregexoophelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mjackson11
    wrote on last edited by
    #1

    I have a simple base class that holds id's for use in sorting other classes. It is a non-templated class in an single hpp file ( indexedObject, see below ) class indexedObject { public: indexedObject() : _mID(BAD_DB_ID) {} indexedObject(dbID id) : _mID(id) {} ~indexedObject(); dbID getID(void); void setID(dbID id); bool operator<(const dbID& param) const; bool operator>(const dbID& param) const; bool operator==(const dbID& param) const; bool operator!=(const dbID& param) const; protected: dbID _mID; }; // indexedObject inline indexedObject::~indexedObject() { _mID = BAD_DB_ID; } inline dbID indexedObject::getID(void) { return _mID; } inline void indexedObject::setID(dbID id) { _mID = id; } inline bool indexedObject::operator<(const dbID& param) const { return (_mID < param); } inline bool indexedObject::operator>(const dbID& param) const { return (_mID > param); } inline bool indexedObject::operator==(const dbID& param) const { return (_mID == param); } inline bool indexedObject::operator!=(const dbID& param) const { return (_mID != param); } I have a templated class that inherits from the base class. template class fwdCurve : public indexedObject, public broadCaster { blah blah... } When I try to define setID in the derived class template void fwdCurve<_Ty>::setID(dbID id) I get a C2244: indexedObject::setID : unable to match function definition to an existing declaration I am able to work around this by declaring setID() in the derived class definition and explicitly calling the base class method but why is this happening? I have VS 2005 with SP1 and hotfix KB930859 installed.

    S 1 Reply Last reply
    0
    • M mjackson11

      I have a simple base class that holds id's for use in sorting other classes. It is a non-templated class in an single hpp file ( indexedObject, see below ) class indexedObject { public: indexedObject() : _mID(BAD_DB_ID) {} indexedObject(dbID id) : _mID(id) {} ~indexedObject(); dbID getID(void); void setID(dbID id); bool operator<(const dbID& param) const; bool operator>(const dbID& param) const; bool operator==(const dbID& param) const; bool operator!=(const dbID& param) const; protected: dbID _mID; }; // indexedObject inline indexedObject::~indexedObject() { _mID = BAD_DB_ID; } inline dbID indexedObject::getID(void) { return _mID; } inline void indexedObject::setID(dbID id) { _mID = id; } inline bool indexedObject::operator<(const dbID& param) const { return (_mID < param); } inline bool indexedObject::operator>(const dbID& param) const { return (_mID > param); } inline bool indexedObject::operator==(const dbID& param) const { return (_mID == param); } inline bool indexedObject::operator!=(const dbID& param) const { return (_mID != param); } I have a templated class that inherits from the base class. template class fwdCurve : public indexedObject, public broadCaster { blah blah... } When I try to define setID in the derived class template void fwdCurve<_Ty>::setID(dbID id) I get a C2244: indexedObject::setID : unable to match function definition to an existing declaration I am able to work around this by declaring setID() in the derived class definition and explicitly calling the base class method but why is this happening? I have VS 2005 with SP1 and hotfix KB930859 installed.

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      mjackson11 wrote:

      When I try to define setID in the derived class template void fwdCurve<_Ty>::setID(dbID id) I get a C2244:   indexedObject::setID : unable to match function definition to an existing declaration

      Yes - this is the way C++ works...you need a method declaration in the class before you can define it. The declaration in the base class does not change this.

      mjackson11 wrote:

      I am able to work around this by declaring setID() in the derived class definition and explicitly calling the base class method but why is this happening?

      If you just want to call the base classes implementation, then you need no method definition or declaration in the derived class. What are you actually trying to accomplish?

      M 1 Reply Last reply
      0
      • S Stuart Dootson

        mjackson11 wrote:

        When I try to define setID in the derived class template void fwdCurve<_Ty>::setID(dbID id) I get a C2244:   indexedObject::setID : unable to match function definition to an existing declaration

        Yes - this is the way C++ works...you need a method declaration in the class before you can define it. The declaration in the base class does not change this.

        mjackson11 wrote:

        I am able to work around this by declaring setID() in the derived class definition and explicitly calling the base class method but why is this happening?

        If you just want to call the base classes implementation, then you need no method definition or declaration in the derived class. What are you actually trying to accomplish?

        M Offline
        M Offline
        mjackson11
        wrote on last edited by
        #3

        Sorry, to be more specific, I tried to implement setID() in the derived class without putting setID in the class declaration. That's when it threw the error. If I define setID in the derived class declaration, when I implement it, it does not call the base class setID method. Seems like the compiler is defining them as two different methods instead of the derived class inhereting.

        S M 3 Replies Last reply
        0
        • M mjackson11

          Sorry, to be more specific, I tried to implement setID() in the derived class without putting setID in the class declaration. That's when it threw the error. If I define setID in the derived class declaration, when I implement it, it does not call the base class setID method. Seems like the compiler is defining them as two different methods instead of the derived class inhereting.

          S Offline
          S Offline
          Stuart Dootson
          wrote on last edited by
          #4

          mjackson11 wrote:

          If I define setID in the derived class declaration, when I implement it, it does not call the base class setID method. Seems like the compiler is defining them as two different methods instead of the derived class inhereting.

          Yep, that's the way most (if not all) OO languages work. The point is that if you do want to call the base classes method, you can very easily. However, if the language automatically called the base classes methods and you didn't want that to happen, you're stuffed. Anyway - in conclusion, what you've described is the way C++ is meant to work.

          1 Reply Last reply
          0
          • M mjackson11

            Sorry, to be more specific, I tried to implement setID() in the derived class without putting setID in the class declaration. That's when it threw the error. If I define setID in the derived class declaration, when I implement it, it does not call the base class setID method. Seems like the compiler is defining them as two different methods instead of the derived class inhereting.

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            I wasn't entirely clear about the one case when base class methods are called automatically. If a base class implements a method and your derived class doesn't declare or define it, then the base class method is automatically called. Consider this code: #include <iostream> class Base { public: void A() { std::cout << "Base::A\n"; } void B() { std::cout << "Base::A\n"; } virtual void C() { std::cout << "Base::C\n"; } }; class Derived : public Base { public: void A() { std::cout << "Derived::A\n"; } virtual void C() { std::cout << "Derived::C\n"; } }; void VirtualTest(Base* pBase, const char* objName) { std::cout << "Calling pBase->A() with pBase = " << objName << "...\n"; pBase->A(); std::cout << "Calling pBase->C() with pBase = " << objName << "...\n"; pBase->C(); } int main(int, char**) { Base base; Derived derived; std::cout << "Calling base.A()...\n"; base.A(); std::cout << "Calling base.B()...\n"; base.B(); VirtualTest(&base, "&base"); std::cout << "Calling derived.A()...\n"; derived.A(); std::cout << "Calling derived.B()...\n"; derived.B(); VirtualTest(&derived, "&derived"); } You should get this output:

            Calling base.A()...
            Base::A
            Calling base.B()...
            Base::A
            Calling pBase->A() with pBase = &base...
            Base::A
            Calling pBase->C() with pBase = &base...
            Base::C
            Calling derived.A()...
            Derived::A
            Calling derived.B()...
            Base::A
            Calling pBase->A() with pBase = &derived...
            Base::A
            Calling pBase->C() with pBase = &derived...
            Derived::C

            So - Derived overrides the A() and C() from Base and inherits B() from Base. The difference between overriding virtual and non-virtual methods is demonstrated in VirtualTest - when you have a pointer to base, Base::A() gets called because A() is non-virtual, whereas the implementation of C() that is called depends on the actual type of the object that is pointed to, not the type of the pointer.

            M 1 Reply Last reply
            0
            • M mjackson11

              Sorry, to be more specific, I tried to implement setID() in the derived class without putting setID in the class declaration. That's when it threw the error. If I define setID in the derived class declaration, when I implement it, it does not call the base class setID method. Seems like the compiler is defining them as two different methods instead of the derived class inhereting.

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #6

              In addition to Stuart's replies... You haven't used "virtual' anywhere, so a method implementation in a derived class has nothing to do with a base class' method. Even if you use "virtual", you'd still need to include botht the definition and the implementation in the derived class, and call the base class method explicitly if desired. Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              1 Reply Last reply
              0
              • S Stuart Dootson

                I wasn't entirely clear about the one case when base class methods are called automatically. If a base class implements a method and your derived class doesn't declare or define it, then the base class method is automatically called. Consider this code: #include <iostream> class Base { public: void A() { std::cout << "Base::A\n"; } void B() { std::cout << "Base::A\n"; } virtual void C() { std::cout << "Base::C\n"; } }; class Derived : public Base { public: void A() { std::cout << "Derived::A\n"; } virtual void C() { std::cout << "Derived::C\n"; } }; void VirtualTest(Base* pBase, const char* objName) { std::cout << "Calling pBase->A() with pBase = " << objName << "...\n"; pBase->A(); std::cout << "Calling pBase->C() with pBase = " << objName << "...\n"; pBase->C(); } int main(int, char**) { Base base; Derived derived; std::cout << "Calling base.A()...\n"; base.A(); std::cout << "Calling base.B()...\n"; base.B(); VirtualTest(&base, "&base"); std::cout << "Calling derived.A()...\n"; derived.A(); std::cout << "Calling derived.B()...\n"; derived.B(); VirtualTest(&derived, "&derived"); } You should get this output:

                Calling base.A()...
                Base::A
                Calling base.B()...
                Base::A
                Calling pBase->A() with pBase = &base...
                Base::A
                Calling pBase->C() with pBase = &base...
                Base::C
                Calling derived.A()...
                Derived::A
                Calling derived.B()...
                Base::A
                Calling pBase->A() with pBase = &derived...
                Base::A
                Calling pBase->C() with pBase = &derived...
                Derived::C

                So - Derived overrides the A() and C() from Base and inherits B() from Base. The difference between overriding virtual and non-virtual methods is demonstrated in VirtualTest - when you have a pointer to base, Base::A() gets called because A() is non-virtual, whereas the implementation of C() that is called depends on the actual type of the object that is pointed to, not the type of the pointer.

                M Offline
                M Offline
                mjackson11
                wrote on last edited by
                #7

                My error, I got myself confused. Everything is working correctly. I had gotten confused with constructors that call the base class constructor. I re-declared everything and explicitly called the base class method.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups