如何将类对象push_back到 std::vector 中

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

本文关键字:std vector back 对象 push      更新时间:2023-10-16

我在弄清楚一些事情时遇到了一些麻烦。基本上我有2类,每当我创建对象时,它都可以正常工作。但是,当我尝试push_back main()函数中的向量时,它会返回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;
 }

"但是,当我尝试push_back MAIN 函数中的向量时,它 返回 0 0 0(B 默认值(">

这是因为没有初始化类B成员变量。当您将新的A对象push_backstd::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;
}