Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何将类对象推回std::vector?_C++_Class_C++11_Segmentation Fault_Stdvector - Fatal编程技术网

C++ 如何将类对象推回std::vector?

C++ 如何将类对象推回std::vector?,c++,class,c++11,segmentation-fault,stdvector,C++,Class,C++11,Segmentation Fault,Stdvector,我搞不清楚一些事情。基本上我有2个类,每当我创建对象时,它都工作得很好。但是,当我尝试在main()函数中将推回到一个向量时,它返回0(B默认值),如果我尝试创建一个空函数,它将返回分段错误。有什么想法吗 class Date { public: Date(int day=0, int month=0, int year=0) : _day(day), _month(month),_year(year) {} int get_day() { return _day; }

我搞不清楚一些事情。基本上我有
2个
类,每当我创建对象时,它都工作得很好。但是,当我尝试在
main()
函数中将
推回
到一个向量时,它返回
0
(B默认值),如果我尝试创建一个空函数,它将返回分段错误。有什么想法吗

class Date
{
 public:
   Date(int day=0, int month=0, int year=0) : _day(day), _month(month),_year(year) {}
   int get_day()     { return _day; }
   int get_month()   { return _month; }
   int get_year()    { return _year; }
   void writestuff() { std::cout << _day << "/" << _month << "/" << _year<< std::endl; }
   ~Date(){}
 private:
   int _day;
   int _month;
   int _year;
 };

 class Adatok
 {
 public:
   Adatok(std::string name, std::string path, Date date ): _name(name), _path(path), _date(date) {}
   void writestuff()
   {
      std::cout<<_name<<" "<<_path<<" ";
      _date.writestuff();
      std::cout<<std::endl;
   }
   Adatok(const Adatok& other){}
   Adatok operator= (const Adatok& other){}
   ~Adatok(){}

 private:
   std::string _name;
   std::string _path;
   Date _date;
 };  

 void database(std::string& temp, std::vector<Adatok> my_vec); // this would be the segmentation fault code, it's not implemented anymore

 int main(int argc, char **argv)
 {
   std::vector<Adatok> my_vec;
   std::string temp;
   boost::filesystem::ifstream input_file("input");
    while (getline(input_file, temp))
    {
     //---------------------------------don't mind theese------------------------------------------------------------------
      temp += ',';
      std::string name = temp.substr(temp.find_first_of('"'),temp.find_first_of(','));
      temp.erase(0, name.length() + 1);
      std::string path = temp.substr(temp.find_first_of('"'),temp.find_first_of(','));
      temp.erase(0, path.length() + 1);
      std::string numbers(temp.substr(temp.find_first_of('"') + 1,temp.find_first_of('-')));
      int year, month, day;
      year = std::atoi(numbers.c_str());
      temp.erase(0, temp.find_first_of('-') + 1);
      numbers = temp.substr(0, temp.find_first_of('-'));
      month = std::atoi(numbers.c_str());
      temp.erase(0, temp.find_first_of('-') + 1);
      numbers = temp.substr(0, temp.find_first_of(' '));
      day = std::atoi(numbers.c_str());
      //Date obj(day, month, year);
      //Adatok elem(name, path, obj);
      //---------------------------------------don't mind theese-----------------------------------------------------------------
      my_vec.push_back(Adatok(name,path,Date(day,month,year))); //probably fails
     }
       for(std::vector<Adatok>::iterator it{my_vec.begin()};it !=my_vec.end();it++)
       it -> writestuff();
       return 0;
 }
上课日期
{
公众:
日期(整数天=0,整数月=0,整数年=0):\u天(天),\u月(月),\u年(年){
int get_day(){return_day;}
int get_month(){return_month;}
int get_year(){return_year;}
void writestuff(){std::cout
“然而,当我试图将_推回主函数中的向量时,它
返回0(B默认值)”

这是因为没有初始化
B
类的成员变量。这应该在
将新
a
对象推回
std::vector
时执行,如下所示:

vecA.push_back(A("name", "path", B(15, 04, 2018)));
如果您的疑问是如何使用
push_back
is,以上肯定会澄清

更新:我已将
复制构造函数
复制赋值运算符
设置为
默认值
,并且它工作正常。实时操作:

#包括
#包括
#包括
上课日期
{
公众:
日期(整数天=0,整数月=0,整数年=0)
:(日),(月),(年){}
~Date(){}
int get_day(){return_day;}
int get_month(){return_month;}
int get_year(){return_year;}
void writestuff()
{

std::难道你没有实际显示任何失败的代码吗?如果我在一个:void函数(temp&,my_vec)中放入完全相同的代码,它会产生分段错误现在它不完整了…请发布一个(从上到下阅读此链接页)。您可能不需要显示
Adatok
Date
代码。他们的复制构造函数可能有问题。完成后,我为我造成的麻烦感到抱歉…我以前已经尝试过这些:),不幸的是,它不起作用。我希望我做得对,我对社区是相当陌生的
#include <iostream>
#include <string>
#include <vector>

class Date
{
 public:
    Date(int day = 0, int month = 0, int year = 0)
        : _day(day), _month(month),_year(year) {}
    ~Date(){}

    int get_day() { return _day; }
    int get_month() { return _month; }
    int get_year() { return _year; }
    void writestuff()
    {
       std::cout << _day << "/" << _month << "/" << _year<< std::endl;
    }
 private:
    int _day;
    int _month;
    int _year;
 };

 class Adatok
 {
 public:
    Adatok(std::string name, std::string path, Date date )
        : _name(name), _path(path), _date(date) {}
    ~Adatok(){}
    void writestuff()
    {
        std::cout<<_name<<" "<<_path<<" ";
        _date.writestuff();
        std::cout<<std::endl;
    }
    //change in copy constructor and copy assignment operator
    Adatok(const Adatok& other) = default;
    Adatok& operator= (const Adatok& other) = default;
 private:
   std::string _name;
   std::string _path;
   Date _date;
 };

void database(std::string temp, std::vector<Adatok> my_vec)
{
    for(auto& it: my_vec)
       it.writestuff();
}
int main(int argc, char **argv)
{
    std::vector<Adatok> my_vec;
    int year = 2018, month = 04, day = 15;
    std::string name = "name1", path = "path1";
    my_vec.push_back(Adatok(name,path,Date(day,month,year)));

    database("something", my_vec);

    return 0;
}