-4

I have a virtual base class, base_class, that uses as attribute a class type object of class colours. From the virtual base class, I derived another class, derived_class.

The debugger doesn't show any errors, but my program is not compiling. I found that if I remove the virtual function declaration in the base_class, the whole program compiles just fine. Why is this? I wanted to keep the virtual function, how can I do it?

Here is the code:

class colours {
protected: 
    std::vector<double> colour1, colour2; 
public:
    colours() {std::cout << "Calling colours default constructor" <<std::endl;}
    colours(std::vector<double> c1, std::vector<double> c2) : colour1{c1}, colour2{c2} {} 
    ~colours(){}
};

class base_class {
protected:
    std::string name{"unnamed"};
    colours col;
public:
    base_class() {std::cout << "Calling base default constructor" <<std::endl;}
    base_class(std::string const n, colours const c) : name{n}, col{c} {} 
    virtual ~base_class(){} 
    
    virtual void do_something();
};

class derived_class : public base_class {
public:
    derived_class() : base_class() {std::cout << "Calling derived default constructor" <<std::endl;} // This is what makes my code not work
    derived_class(std::string const n, colours const c) : base_class{n, c} {} 
    ~derived_class(){}

    void do_something();
};

void derived_class::do_something() {
    std::cout << "I am doing something";
}

int main() {
    derived_class test;
    return 0;
}
user
  • 3
  • 2
  • 5
    What is the error you get? Compilation never stops working without an error – UnholySheep May 11 '22 at 15:51
  • @UnholySheep it stops without errors, don't know what to say – user May 11 '22 at 15:54
  • Most likely issue: `virtual void do_something();` should be `virtual void do_something() = 0;` – UnholySheep May 11 '22 at 15:54
  • 2
    *"it stops without errors"* - I find that hard to believe unless you have a completely broken compiler – UnholySheep May 11 '22 at 15:54
  • 2
    Re: "I have a virtual base class `base_class`" -- the code doesn't have any virtual base classes. All of the inheritance is direct. For example, `class derived_class : public base_class`. If `base_class` was a virtual base it would say so: `class derived_class : public virtual base_class`. – Pete Becker May 11 '22 at 15:55
  • [The build does NOT stop without errors](https://godbolt.org/z/dbccPd3eo). Unless you literally mean there are no compiler errors, which technically does hold – Lala5th May 11 '22 at 15:56
  • I get no compiler errors. I do get linker errors. `base_class::do_something` definition is missing. – Eljay May 11 '22 at 16:00
  • @Lala5th Why is it missing the vtable? The class has a virtual destructor. I could see it complaining about a missing `void base_class::do_something()`. But vtable I find odd. Note: `virtual void do_something() = 0;` fixes the error. – Goswin von Brederlow May 11 '22 at 16:01
  • @GoswinvonBrederlow • most compilers emit the virtual function table with the first non-inline virtual function definition. If there are no non-inline virtual functions, it is emitted with every translation unit with that class. – Eljay May 11 '22 at 16:02
  • @GoswinvonBrederlow [Relevant question](https://stackoverflow.com/questions/3065154/undefined-reference-to-vtable) and a [very good answer](https://stackoverflow.com/a/57504289/10055166) – Lala5th May 11 '22 at 16:03
  • This is all useful, thanks, fixed – user May 11 '22 at 16:15

0 Answers0