简体   繁体   中英

C++ Modifying class object within a vector

My issue is my last increment_watch function. I would like to be able modify parts of the Movie class while it is in a vector container. 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. I would like to be able to modify or add +1 to the watched element. Is this at all possible?

EDIT: I have updated the "increment_watched" function taking suggestions from the comments. 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.

#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. You response lead the to the correct output. I wanted to change the values and completely over looked passing the vec variable to pass by reference. 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. 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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