簡體   English   中英

C++ 在向量內修改 class object

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

我的問題是我的最后一個 increment_watch function。 我希望能夠修改位於矢量容器中的電影 class 的部分內容。 現在我只能獲取特定的電影 object 及其增量,但當它位於向量中時,我無法更改或修改其中的任何內容。 我希望能夠修改或添加 +1 到被監視的元素。 這是可能嗎?

編輯:我已經更新了“increment_watched” function 從評論中獲取建議。 我試圖通過引用來傳遞監視變量,但是 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;
}

謝謝用戶 WhozCraig,這讓我回答了我的問題。 您的響應導致正確的 output。 我想更改值並完全忽略了傳遞 vec 變量以通過引用傳遞。 我知道這是我在看的小東西

對於所有發布有用評論以幫助在網站上發布更好問題的人,謝謝。 我是 StackOverflow 的新手。 我將查看提供的有用鏈接,以便將來發布更好的問題。 有人評論說我發了兩次同樣的問題,我驚慌失措並刪除了帖子,因為它被鎖定了,我不知道該怎么辦,所以我想我會重新開始。

這是更正后的代碼:

#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