我在搞清楚一些事情时遇到了一些麻烦。基本上我有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;
}
答案 0 :(得分:1)
&#34;但是当我尝试将push_back推送到MAIN函数中的向量时,它 返回0 0 0(B默认值)&#34;
这是因为没有初始化B
类的成员变量。这应该在push_back
A
std::vector
对象vecA.push_back(A("name", "path", B(15, 04, 2018)));
之后执行此操作:
push_back
如果您对如何使用copy constructor
表示怀疑,上面肯定会澄清它。
更新:我已将copy assignment operator
和default
设置为#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;
}
并且有效。实时行动:https://www.ideone.com/TlmAm2
db:seed