该功能在获取对象向量时未获得向量。

huangapple go评论73阅读模式
英文:

the function doesn't get a vector when getting a whole vector of objects

问题

saveEverything 函数获取了用户向量,但没有获取用户的运动向量(它是用户的一部分),因此无法将信息保存到文件。这是由什么引起的?

GitHub链接

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?

github link

saveEverything function:

void saveEverything(std::vector&lt;User&gt; users){
std::fstream users_database;
users_database.open(&quot;users_database.txt&quot;,std::ios::out | 
std::ios::trunc);
if(!users_database)
    throw 1;
for(int i =0;i&lt;users.size();i++){
    users_database&lt;&lt;users[i].getUserFirstName()&lt;&lt;&quot;|&quot; 
&lt;&lt;users[i].getUserLastname()&lt;&lt;&quot;|&quot;&lt;&lt;users[i].getUserAge()&lt;&lt;&quot;|&quot; 
&lt;&lt;users[i].getUserWeight()&lt;&lt;std::endl;
}
users_database&lt;&lt;&quot;||ENDOFUSERS||&quot;&lt;&lt;std::endl;

std::vector&lt;Exercise&gt; exercises;

for(int i=0;i&lt;users.size();i++){
    users_database&lt;&lt;&quot;||U||&quot;&lt;&lt;std::endl;
    users_database&lt;&lt;users[i].getUserFirstName()&lt;&lt;&quot;|&quot; 
&lt;&lt;users[i].getUserLastname()&lt;&lt;std::endl;
    exercises = users[i].getExercises();
    for(Exercise exercise: exercises){
        users_database&lt;&lt;exercise.getExerciseName()&lt;&lt;&quot;|&quot; 
&lt;&lt;exercise.getExerciseWeight()&lt;&lt;&quot;|&quot;&lt;&lt;exercise.getExerciseSeries() 
&lt;&lt;&quot;|&quot;&lt;&lt;exercise.getExerciseReps()&lt;&lt;std::endl;
    }
    users_database&lt;&lt;&quot;||END||&quot;&lt;&lt;std::endl;
}

std::cout&lt;&lt;&quot;Saving completed!&quot;&lt;&lt;std::endl;
}

class User:

class User {
friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, const User 
&amp;user);
private:
std::string UserFirstName;
std::string UserLastname;
unsigned int UserAge;
double UserWeight;

std::vector&lt;Exercise&gt;UserExercises;

getExercise() method:

std::vector&lt;Exercise&gt; 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 &copy_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 &amp;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.

huangapple
  • 本文由 发表于 2023年3月31日 23:20:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75900185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定