如何将push_back一个class对象变成std::vector?

How to push_back a class object into a std::vector?

我在弄清楚一些事情时遇到了一些困难。基本上我有 2 类,每当我创建对象时,它都可以正常工作。但是,当我尝试 push_backmain() 函数中的向量时,它 return 0 0 0 (B 默认值),如果我尝试创建一个 void 函数,这将这样做,它会返回分段错误。有什么想法吗?

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

"however when i try to push_back to a vector in MAIN function, it return 0 0 0 (B default values)"

这是因为没有初始化Bclass的成员变量。这应该在你 push_back 一个新的 A 对象到 std::vector 时完成,如下所示:

vecA.push_back(A("name", "path", B(15, 04, 2018)));

如果你的疑问是push_back是如何使用的,上面一定会说明的。

更新:我已将 copy constructorcopy assignment operator 设置为 default 并且有效。真人实拍:https://www.ideone.com/TlmAm2

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