简体   繁体   English

C++ 在向量内修改 class object

[英]C++ Modifying class object within a vector

My issue is my last increment_watch function.我的问题是我的最后一个 increment_watch function。 I would like to be able modify parts of the Movie class while it is in a vector container.我希望能够修改位于矢量容器中的电影 class 的部分内容。 Right now I am only able to grab a specific Movie object and its increment but I am unable to change or modify any of it while it is in a vector.现在我只能获取特定的电影 object 及其增量,但当它位于向量中时,我无法更改或修改其中的任何内容。 I would like to be able to modify or add +1 to the watched element.我希望能够修改或添加 +1 到被监视的元素。 Is this at all possible?这是可能吗?

EDIT: I have updated the "increment_watched" function taking suggestions from the comments.编辑:我已经更新了“increment_watched” function 从评论中获取建议。 I have tried to pass by reference the watched variable but the "mov.set_watched" call in the increment_watched function is not taking effect.我试图通过引用来传递监视变量,但是 increment_watched function 中的“mov.set_watched”调用没有生效。

#include <iostream>
#include <vector>
#include <string>

class Movie {
    friend std::ostream &operator<<(std::ostream &os, const Movie &p);
private:
    std::string name;
    std::string ratting;
    int watched;
public:
    Movie() = default;
    Movie(std::string name, std::string ratting, int watched) 
        : name{name}, ratting{ratting}, watched{watched}  {}

    void increment_watched(std::vector<Movie> vec, std::string name);

    bool operator<(const Movie &rhs) const {                       // always overload
        return this->watched < rhs.watched;
    }
    bool operator==(const Movie &rhs) const {
        return (this->name == rhs.name && this->watched == rhs.watched);    // always overload
    }

    void set_name(std::string name) {
        this->name = name; 
    }

    std::string getName() { 
        return name; 
    }

    void set_ratting(std::string ratting) {
        this->ratting = ratting; 
    }

    std::string getRatting() { 
        return ratting; 
    }

    void set_watched(int watched) {
        this->watched = watched; 
    }

    int getWatched() { 
        return watched; 
    }

};

std::ostream &operator<<(std::ostream &os, const Movie &p) {
    os << p.name << " : " << p.ratting << " : " << p.watched;
    return os;
}

// template function to display any vector
template <typename T>
void display(const std::vector<T> &lem) {
    std::cout << "[ ";
    for (const auto &elem: lem)
        std::cout << elem << " ";
    std::cout <<  " ]"<< std::endl;
}

// I want to modify this function to increment the "watched" variable by one
void increment_watched(std::vector<Movie> vec, std::string name, int &watched){
    watched =+ 1;
    for (auto &mov: vec){
        if (mov.getName() == name){
            std::cout << name << std::endl;
            mov.set_watched(watched); 
        }
    }
}

int main() {

    std::vector<Movie> vec;
    int watched = 1;

    Movie p1 {"Star Wars", "PG", 2};
    Movie p2 {"Indiana Jones", "PG", 1};
    Movie p3 {"Matrix", "PG-13", 5};

    vec.push_back(p2);
    vec.push_back(p3);

    std::cout << p1.getName() << std::endl;
    std::cout << p1.getRatting() << std::endl;
    
    p1.set_watched(100);
    vec.push_back(p1);
    
    std::cout << p1.getWatched() << std::endl;
    display(vec);

     
    increment_watched(vec, "Star Wars", watched);
    display(vec);

    return 0;
}

Thank you user WhozCraig, this lead me to answering my question.谢谢用户 WhozCraig,这让我回答了我的问题。 You response lead the to the correct output.您的响应导致正确的 output。 I wanted to change the values and completely over looked passing the vec variable to pass by reference.我想更改值并完全忽略了传递 vec 变量以通过引用传递。 I knew it was something small that I was over looking我知道这是我在看的小东西

To all those who posted helpful comments to help post better questions on the site, thank you.对于所有发布有用评论以帮助在网站上发布更好问题的人,谢谢。 I am new to StackOverflow.我是 StackOverflow 的新手。 I will review the provided helpful links in order to post better questions in the future.我将查看提供的有用链接,以便将来发布更好的问题。 Some commented that I posted the same question twice, I panicked and deleted the post because it was locked, I didnt know what to do so I thought I would start over.有人评论说我发了两次同样的问题,我惊慌失措并删除了帖子,因为它被锁定了,我不知道该怎么办,所以我想我会重新开始。

Here is the corrected code:这是更正后的代码:

#include <iostream>
#include <vector>
#include <string>

class Movie {
    friend std::ostream &operator<<(std::ostream &os, const Movie &p);
private:
    std::string name;
    std::string ratting;
    int watched;
public:
    Movie() = default;
    Movie(std::string name, std::string ratting, int watched) 
        : name{name}, ratting{ratting}, watched{watched}  {}

    void increment_watched(std::vector<Movie> vec, std::string name);

    bool operator<(const Movie &rhs) const {                       // always overload
        return this->watched < rhs.watched;
    }
    bool operator==(const Movie &rhs) const {
        return (this->name == rhs.name && this->watched == rhs.watched);    // always overload
    }

    void set_name(std::string name) {
        this->name = name; 
    }

    std::string getName() { 
        return name; 
    }

    void set_ratting(std::string ratting) {
        this->ratting = ratting; 
    }

    std::string getRatting() { 
        return ratting; 
    }

    void set_watched(int watched) {
        this->watched = watched; 
    }

    int getWatched() { 
        return watched; 
    }

};

std::ostream &operator<<(std::ostream &os, const Movie &p) {
    os << p.name << " : " << p.ratting << " : " << p.watched;
    return os;
}

// template function to display any vector
template <typename T>
void display(const std::vector<T> &lem) {
    std::cout << "[ ";
    for (const auto &elem: lem)
        std::cout << elem << " ";
    std::cout <<  " ]"<< std::endl;
}

// I want to modify this function to increment the "watched" variable by one
void increment_watched(std::vector<Movie> &vec, std::string name, int &watched){
    for (auto &mov: vec){
        if (mov.getName() == name){
            std::cout << name << std::endl;
            mov.set_watched(watched + 1); 
        }
    }
}

int main() {

    std::vector<Movie> vec;
    int watched = 3;

    Movie p1 {"Star Wars", "PG", 2};
    Movie p2 {"Indiana Jones", "PG", 1};
    Movie p3 {"Matrix", "PG-13", 5};

    vec.push_back(p2);
    vec.push_back(p3);

    std::cout << p1.getName() << std::endl;
    std::cout << p1.getRatting() << std::endl;
    
    p1.set_watched(100);
    vec.push_back(p1);
    
    std::cout << p1.getWatched() << std::endl;
    display(vec);

     
    increment_watched(vec, "Star Wars", watched);
    display(vec);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM