简体   繁体   English

为什么我在调用构造函数后会崩溃? 我正在尝试将 shared_ptr 推回向量

[英]Why am I getting a crash after calling my constructor? I'm trying to push back a shared_ptr to a vector

I'm quite new to programming (currently learning C++).我对编程很陌生(目前正在学习 C++)。 I'm trying to learn how to use smart pointers.我正在尝试学习如何使用智能指针。

I'm trying to do a challenge from a tutorial in which the goal is to fill in data points to a unique_ptr of a vector that contains shared_ptr s that points to a data point.我正在尝试从教程中进行挑战,其中目标是将数据点填充到向量的unique_ptr中,该向量包含指向数据点的shared_ptr s。 When I run it, it shows 0 errors and 0 warnings so I'm a bit lost on what went wrong since it crashes when I start to add the data(int).当我运行它时,它显示 0 个错误和 0 个警告,所以我对出了什么问题有点迷失,因为当我开始添加数据(int)时它崩溃了。 Would really appreciate some help on what's going on with this code.非常感谢有关此代码的一些帮助。 I'm still confused on using smart pointers.我仍然对使用智能指针感到困惑。

#include <iostream>
#include <memory>
#include <vector>


class Test {
private:
    int data;
public: 
    Test():data(0) {std::cout<<"\tTest constructor ("<< data << ")"<<std::endl;}
    Test(int data): data{data} {std::cout<< "\tTest constructor ("<< data << ")"<<std::endl;}
    int get_data() const {return data;}
    ~Test(){std::cout<<"\tTest destructor ("<<data<<")"<<std::endl;}
};


//Function prototypes
std::unique_ptr<std::vector<std::shared_ptr<Test>>>make();
void fill(std::vector<std::shared_ptr<Test>> &vec, int num);
void display(const std::vector<std::shared_ptr<Test>>&vec);

std::unique_ptr<std::vector<std::shared_ptr<Test>>>make(){
   return std::unique_ptr<std::vector<std::shared_ptr<Test>>>();
}

void fill(std::vector<std::shared_ptr<Test>> &vec, int num){
        int temp;
        for(int i=0; i<num; ++i){
        std::cout<<"Enter the data points for ["<<i<<"]:";
        std::cin>>temp;
          std::shared_ptr<Test>p1 {new Test {temp}};
          vec.push_back(p1);
    }
}

void display(const std::vector<std::shared_ptr<Test>>&vec){
    std::cout<<"--DISPLAYING VECTOR DATA--"<<std::endl;

    for(const auto &i:vec)
        std::cout<<i->get_data()<<std::endl;

}


int main() {
    std::unique_ptr<std::vector<std::shared_ptr<Test>>> vec_ptr;
    vec_ptr = make();
    int num; 
    
    std::cout<<"How many data points do you want to enter: ";
    std::cin >> num;
    fill(*vec_ptr, num);
    display(*vec_ptr);
    return 0;
}

In the function make , return std::unique_ptr<std::vector<std::shared_ptr<Test>>>();在 function make中, return std::unique_ptr<std::vector<std::shared_ptr<Test>>>(); just returns an empty std::unique_ptr which owns nothing.只返回一个空的std::unique_ptr ,它什么都不拥有。 Dereference and usage on it like *vec_ptr leads to undefined behavior.*vec_ptr这样的取消引用和使用会导致未定义的行为。

You could你可以

return std::unique_ptr<std::vector<std::shared_ptr<Test>>>(new std::vector<std::shared_ptr<Test>>);

Or better或更好

return std::make_unique<std::vector<std::shared_ptr<Test>>>();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我能够在C ++中将常量shared_ptr分配给非常量shared_ptr? - Why I am able to assign constant shared_ptr to non constant shared_ptr in C++? 为什么在使用shared_ptr时出现此运行时异常? - why am i getting this runtime exception when using shared_ptr? 我可以将0分配给shared_ptr吗? 为什么? - Can I assign 0 to a shared_ptr? Why? 如何将字符串推送到 shared_ptr 的向量中? - How to push a string into a vector of shared_ptr? 为什么我的构造函数涉及 shared_ptr 不正确? - Why is my constructor involving a shared_ptr incorrect? 为什么/何时应该在std :: vector &lt;&gt;上使用std :: unique / shared_ptr(std :: vector &lt;&gt;)? - Why/when should I use std::unique/shared_ptr (std::vector<>) over just std::vector<>? 如果我在多线程中重置相同的 shared_ptr 不会崩溃 - no crash if I reset the same shared_ptr in multi-threads 如何将 shared_ptr 变量 push_back 到 C++ 中的 shared_ptr 向量? - How do you push_back a shared_ptr variable to a vector of shared_ptrs in C++? 为什么我不能将 unique_ptr 推回到向量中? - Why can I not push_back a unique_ptr into a vector? 当想要push_back向量列表时,shared_ptr的向量导致错误:“没有重载函数的实例” - Vector of shared_ptr resulting in error: “no instance of overloaded function” when wanting to push_back vector list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM