Thread: undefined reference to constructor of parent virtual class

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    23

    undefined reference to constructor of parent virtual class

    Basically, when the compiler tries to link this stuff into a "simpletest" executable (the simpletest.cpp with a main isn't included but I can if needed) it says it can't reference the constructor of the parent class.

    I never call the constructor of the parent class because it's a virtual class, so I am at a loss. Any advice?

    Code:
    //Device.hpp
    
    
    class Device
    {
    public:
            Device();
           virtual DevErrors DevCreateContext(const char*) = 0; 
           virtual void DevDeleteContext() = 0;
           virtual DevErrors DevPrep(Graph **graph, int type) = 0;
           virtual DevErrors DevPrep(Graph **graph, const char *str, int type) = 0;  
           virtual DevErrors DevConfigure(Graph *graph) = 0;
           virtual DevErrors DevInvoke(const DevCallInfo *callinfo, DevGroupFlags flags) = 0;
    };
    
    
    //SimDevice.hpp
    #include "Device.hpp"
    class SimDevice : public Device
    {
    public:
            SimDevice();
            DevErrors DevCreateContext(const char*);
            void DevDeleteContext();
            DevErrors DevPrep(Graph **graph, int type);
            DevErrors DevPrep(Graph **graph, const char *str, int type);
            DevErrors DevConfigure(Graph *graph);
            DevErrors DevInvoke(const DevCallInfo *callinfo, DevGroupFlags flags);
            ~SimDevice();
    private:
            DevContextInfo info;
            DevContextResult result;
    
    
    };
    
    
    //SimDevice.cpp
    #include "SimDevice.hpp"
    SimDevice::SimDevice() // Compiler is saying this line has undefined reference to `Device::Device()'
    {
        /* Do constructor stuff. */
    }
    Last edited by joshuastuden; 08-15-2018 at 06:49 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    When constructing an object, its base class ctor is called, too. By having the following line in class Device you promise to define your own ctor, but you never do. Just take it out if you don't want to define one.
    Code:
           Device();
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Oct 2017
    Posts
    23
    Removing it is not correct. It actually spits out a whole slew of errors now.

    I don't think you can remove a constructor like this. I have actually reverted my code to a previous version where all of this was working.

    DO you really think you can just remove a constructor definition like that? lol.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    My guess is

    Replace
    Code:
    Device();
    with
    Code:
    Device(){;}
    Note: I have yet to learn C++

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by joshuastuden View Post
    DO you really think you can just remove a constructor definition like that? lol.
    Have you never heard of a default constructor?
    Code:
    #include <iostream>
     
    class Base {
    public:
        virtual void func() = 0;
        virtual ~Base() {}
    };
     
    class Derived : public Base {
        int n;
    public:
        Derived(int n) : n(n) {}
        ~Derived() {}
        void func() { std::cout << "func: " << n << '\n'; }
    };
     
    int main() {
        Base *b = new Derived(42);;
        b->func();
        delete b;
    }
    Last edited by Salem; 08-15-2018 at 11:05 PM. Reason: snip swears
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    Oct 2017
    Posts
    23
    Default constructor is different than what you said. As I said, i removed it and got even different errors.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Show your updated code with the Device() constructor removed and the error that the compiler gave you.

  8. #8
    Registered User
    Join Date
    Oct 2017
    Posts
    23
    Too late. I fixed the problem. Your solution was useless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to redefine a constructor from parent class
    By marcoesteves in forum C++ Programming
    Replies: 10
    Last Post: 07-17-2014, 04:47 PM
  2. Virtual base class constructor
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2008, 02:18 AM
  3. virtual base class constructor
    By George2 in forum Windows Programming
    Replies: 1
    Last Post: 03-24-2008, 12:43 AM
  4. Replies: 6
    Last Post: 10-12-2006, 10:39 PM
  5. Virtual Base Class & Constructor :: C++
    By kuphryn in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2002, 03:14 PM

Tags for this Thread