英文:
the function doesn't get a vector when getting a whole vector of objects
问题
saveEverything
函数获取了用户向量,但没有获取用户的运动向量(它是用户的一部分),因此无法将信息保存到文件。这是由什么引起的?
saveEverything
函数:
void saveEverything(std::vector<User> users){
std::fstream users_database;
users_database.open("users_database.txt", std::ios::out | std::ios::trunc);
if(!users_database)
throw 1;
for(int i = 0; i < users.size(); i++){
users_database << users[i].getUserFirstName() << "|"
<< users[i].getUserLastname() << "|" << users[i].getUserAge() << "|"
<< users[i].getUserWeight() << std::endl;
}
users_database << "||ENDOFUSERS||" << std::endl;
std::vector<Exercise> exercises;
for(int i = 0; i < users.size(); i++){
users_database << "||U||" << std::endl;
users_database << users[i].getUserFirstName() << "|"
<< users[i].getUserLastname() << std::endl;
exercises = users[i].getExercises();
for(Exercise exercise : exercises){
users_database << exercise.getExerciseName() << "|"
<< exercise.getExerciseWeight() << "|"
<< exercise.getExerciseSeries() << "|"
<< exercise.getExerciseReps() << std::endl;
}
users_database << "||END||" << std::endl;
}
std::cout << "Saving completed!" << std::endl;
}
User
类:
class User {
friend std::ostream &operator<<(std::ostream &os, const User &user);
private:
std::string UserFirstName;
std::string UserLastname;
unsigned int UserAge;
double UserWeight;
std::vector<Exercise> UserExercises;
getExercise()
方法:
std::vector<Exercise> User::getExercises(){
return UserExercises;
}
您尝试添加拷贝构造函数,但它们没有起作用。也许您有关于原因或如何以其他方式将此信息保存到文件的想法?
英文:
the saveEverything function gets the User vector but not the Exercise vector (which is part of the User) and it doesn't transfer so I can't save the information to the file. What is it caused by?
saveEverything function:
void saveEverything(std::vector<User> users){
std::fstream users_database;
users_database.open("users_database.txt",std::ios::out |
std::ios::trunc);
if(!users_database)
throw 1;
for(int i =0;i<users.size();i++){
users_database<<users[i].getUserFirstName()<<"|"
<<users[i].getUserLastname()<<"|"<<users[i].getUserAge()<<"|"
<<users[i].getUserWeight()<<std::endl;
}
users_database<<"||ENDOFUSERS||"<<std::endl;
std::vector<Exercise> exercises;
for(int i=0;i<users.size();i++){
users_database<<"||U||"<<std::endl;
users_database<<users[i].getUserFirstName()<<"|"
<<users[i].getUserLastname()<<std::endl;
exercises = users[i].getExercises();
for(Exercise exercise: exercises){
users_database<<exercise.getExerciseName()<<"|"
<<exercise.getExerciseWeight()<<"|"<<exercise.getExerciseSeries()
<<"|"<<exercise.getExerciseReps()<<std::endl;
}
users_database<<"||END||"<<std::endl;
}
std::cout<<"Saving completed!"<<std::endl;
}
class User:
class User {
friend std::ostream &operator<<(std::ostream &os, const User
&user);
private:
std::string UserFirstName;
std::string UserLastname;
unsigned int UserAge;
double UserWeight;
std::vector<Exercise>UserExercises;
getExercise() method:
std::vector<Exercise> User::getExercises(){
return UserExercises;
}
i tried to add copy contructors but they didn't work. Maybe you have an idea what is the reason or how else to save this information to the file?
答案1
得分: 1
你的User
复制构造函数有问题
User(const User ©_user)
: UserFirstName{copy_user.UserFirstName},
UserLastname{copy_user.UserLastname}, UserAge{copy_user.UserAge},
UserWeight{copy_user.UserWeight} {}
它无法复制UserExercises
向量。这意味着每次复制User
对象时,你都会得到一个空的UserExercises
向量。
使用向量和字符串等的一个巨大好处是,你不需要编写复制构造函数、赋值运算符和析构函数,因为默认的会处理得很好。
只需删除这个有问题的复制构造函数,你只需要默认的那个。
我还会移除
~User() = default;
这只是浪费空间和按键。
英文:
Your User
copy constructor is broken
User(const User &copy_user)
: UserFirstName{copy_user.UserFirstName},
UserLastname{copy_user.UserLastname}, UserAge{copy_user.UserAge},
UserWeight{copy_user.UserWeight}{}
It fails to copy the UserExercises
vector. This means that everytime you copy a User
object you get an empty UserExercises
vector.
One large benefit of using vectors and strings etc. is that you don't have to write copy constructors, assignment operators and destructors because the default ones do the right thing.
Just delete this bugged copy constructor, the default one is all you need.
I would also remove
~User()= default;
which is just wasted space and keystrokes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论