C++ cast derived class pointer from base class pointer

  c++, unique-ptr

The following code: https://godbolt.org/z/KKTaGq6hb

#include<memory>
#include<vector>

class Base {
  public:
    virtual bool getStatus() {
      return false;
    }
};
class Derived1 {
  protected:
    bool status;
  public:
    Derived1(): status(false) {}
    void setStatus(bool newStatus) {
      status = newStatus;
    }
    bool getStatus() {
      return status;
    }
};

int main() {
  std::vector<std::unique_ptr<Base>> vars;

  vars.push_back(std::make_unique<Derived1>());
  if(!vars.back()->getStatus()) {
    vars.back()->setStatus(true);
  }
}

Is giving me this compilation error:

<source>: In function 'int main()':
<source>:26:17: error: no matching function for call to 'std::vector<std::unique_ptr<Base> >::push_back(std::_MakeUniq<Derived1>::__single_object)'
   26 |   vars.push_back(std::make_unique<Derived1>());
      |   ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/vector:67,
                 from <source>:2:
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_vector.h:1187:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::unique_ptr<Base>; _Alloc = std::allocator<std::unique_ptr<Base> >; std::vector<_Tp, _Alloc>::value_type = std::unique_ptr<Base>]'
 1187 |       push_back(const value_type& __x)
      |       ^~~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_vector.h:1187:35: note:   no known conversion for argument 1 from 'std::_MakeUniq<Derived1>::__single_object' {aka 'std::unique_ptr<Derived1, std::default_delete<Derived1> >'} to 'const value_type&' {aka 'const std::unique_ptr<Base>&'}
 1187 |       push_back(const value_type& __x)
      |                 ~~~~~~~~~~~~~~~~~~^~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_vector.h:1203:7: note: candidate: 'void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::unique_ptr<Base>; _Alloc = std::allocator<std::unique_ptr<Base> >; std::vector<_Tp, _Alloc>::value_type = std::unique_ptr<Base>]'
 1203 |       push_back(value_type&& __x)
      |       ^~~~~~~~~
/opt/compiler-explorer/gcc-11.2.0/include/c++/11.2.0/bits/stl_vector.h:1203:30: note:   no known conversion for argument 1 from 'std::_MakeUniq<Derived1>::__single_object' {aka 'std::unique_ptr<Derived1, std::default_delete<Derived1> >'} to 'std::vector<std::unique_ptr<Base> >::value_type&&' {aka 'std::unique_ptr<Base>&&'}
 1203 |       push_back(value_type&& __x)
      |                 ~~~~~~~~~~~~~^~~
<source>:28:18: error: 'class Base' has no member named 'setStatus'; did you mean 'getStatus'?
   28 |     vars.back()->setStatus(true);
      |                  ^~~~~~~~~
      |                  getStatus

My grasp of what the error message means is that there’s an object being allocated somehow when I attempt to reference vars.back(). Which doesn’t make sense to me.

Could someone explain? I’m not familiar enough with C++ to groq this.

Source: Windows Questions C++

LEAVE A COMMENT