【问题标题】:How to push_back a class object into a std::vector?如何将类对象 push_back 到 std::vector 中?
【发布时间】:2018-04-15 06:49:50
【问题描述】:

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

【问题讨论】:

  • 您实际上并没有显示任何失败的代码
  • 已编辑,如果我将完全相同的代码放入 : void function(temp&,my_vec) 它会给出分段错误
  • 现在不完整....请发minimal reproducible example(从上到下阅读此链接页面)。
  • 您可能还需要显示AdatokDate 代码。他们的复制构造函数等可能有问题。
  • 完成,很抱歉给我带来的麻烦......

标签: c++ class c++11 segmentation-fault stdvector


【解决方案1】:

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

这是因为没有初始化B类的成员变量。这应该在你 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;
}

【讨论】:

  • 我之前已经试过了:),不幸的是它没有用
  • 我希望我做得对,我对社区还很陌生
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-21
  • 2011-01-17
  • 1970-01-01
  • 2013-10-15
  • 2021-06-18
  • 2023-03-21
  • 1970-01-01
相关资源
最近更新 更多