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;
}