in Education by
I'm relatively new to C++, and I'm hoping someone can help me resolve an issue I'm having with unique_ptr and vectors. Essentially I'm trying to use polymorphism so that I have a vector of type "Base", which is an abstract class (pure virtual). I'm then attempting to fill this vector with derived classes. I've included a trivial example below, showcasing exactly what I'm trying to achieve. Please note that I need to use C++11, which is why I haven't made use of "std::make_unique". The code compiles fine, but I get run-time errors about "default_delete" in class Animal. A related question is should I be using unique_ptrs for run-time polymorphism as below? Or should I be using raw pointers instead? Header file and CPP files below. Error output from VS is included below this. Very many thanks in advance for any help with this. HEADER FILE: #ifndef START_H #define START_H #include #include class Animal { public: virtual ~Animal() = default; void run(); void setNumLegs(int a) { numLegs = a; } const int getLegs() const { return numLegs; } private: double numLegs; virtual void useLegs() = 0; }; class Biped : public Animal { private: void useLegs(); }; class Multiped : public Animal { public: double costOfShoes{ 12.0 }; private: void useLegs(); void payForShoes(); void becomeDestitute(); }; class Farm { public: std::vector> animals; }; class Countryside { public: std::vector> farms; }; #endif // START_H CPP FILE: #include "start.h" #include int main() { Countryside countryside; std::unique_ptr f(new Farm); std::vector legs = { 2,4,5,2,10 }; for (auto& numLegs : legs) { if (numLegs == 2) { std::unique_ptr biped(new Biped); biped->setNumLegs(numLegs); f->animals.push_back(std::move(biped)); } else if (numLegs > 2) { std::unique_ptr multiped(new Multiped); multiped->setNumLegs(numLegs); f->animals.push_back(std::move(multiped)); } } countryside.farms.push_back(std::move(f)); //THIS IS WHERE THE PROBLEM IS I THINK for (auto& animal : f->animals) { animal-> run(); } return 0; } void Animal::run() { useLegs(); } void Biped::useLegs() { std::cout << "Running with: "<< getLegs() <<"legs\n"; } void Multiped::useLegs() { std::cout << "Running with many legs:" << getLegs() << "!!! legs\n"; payForShoes(); } void Multiped::payForShoes() { std::cout << "Paying for shoes...\n"; becomeDestitute(); } void Multiped::becomeDestitute() { std::cout << "I have no money left.\n"; } DEBUGGER ERROR OUTPUT: _Mypair <struct at NULL> std::_Compressed_pair>>,std::_Vector_val>>>,1> _Mypair._Myval2 std::_Vector_val>>> this 0x00000000 std::vector>,std::allocator>>> * JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Hi the problem is you are deferencing a null pointer in the for loop (f is set to nullptr). The move operation moves the ownership of the item pointed to and the pointer f is set to nullptr (move semantics) After a move the object can no longer be used

Related questions

0 votes
    I work on graphics applications and have been using shared & unique pointers essentially because it handles memory ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    I am a beginner and I just need a bit of help on why I getline is showing an error: this is what I have so far ... payments[MAX_ITEMS]; ifstream iFile; if ( argc != 2 ) { cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I am a beginner and I just need a bit of help on why I getline is showing an error: this is what I have so far ... payments[MAX_ITEMS]; ifstream iFile; if ( argc != 2 ) { cout...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    If two vectors with different lengths perform some operation, the elements of the shorter vector will be used ... of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    The current user defined objects like lists, vectors, etc. is referred to as __________ in the R language. ( ... of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    The code below copies data from one vector into another. If the vectors are large then I guess this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    The code below copies data from one vector into another. If the vectors are large then I guess this ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 21, 2022 in Education by JackTerrance
0 votes
    Is there a side effect in doing this: C code: struct foo { int k; }; int ret_foo(const struct ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 13, 2022 in Education by JackTerrance
0 votes
    // merge.cpp // merges two containers into a third #include #include //for merge() using namespace std; int src1[] = { 2, 3, 4, ... , src1+5, src2, src2+3, dest); for(int j=0; j...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    I've been reading the legacy code,which invloves in the customized memory pooling system, then I found that ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    We see lot of sorting techniques like Merge, quick, Heap. Could you help me decide which of these ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I am currently working on a personal project in C++, and I'm having a lot of fun learning. I just learned some ... MainMenu::~MainMenu(){} void MainMenu::displayMenu() { std::cout...
asked May 3, 2022 in Education by JackTerrance
0 votes
    I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 29, 2022 in Education by JackTerrance
0 votes
    I have a c++11 type alias: using coord = std::array; Can I define the operator + for coord? ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
...