指向对象的指针的私有矢量.如何使用getter方法访问这些对象

A private vector of pointers pointing to objects. How to access those objects with getter method?

本文关键字:对象 getter 何使用 方法 访问 指针      更新时间:2023-10-16

我的程序是一个循环的六次迭代,其中八个人相互投票。每个人在每次迭代期间投票给谁被保存到私有类成员voteList(指针的向量(。

我的问题是,在六次迭代结束时,我希望能够使用我编写的GetVote(int)公共方法,说出Anna在每次投票中投票给了谁。

我认为*(voteList[round])应该是安娜在某一轮投票中投票给谁的价值(一个人(?并且使用GetName()方法应该检索那个人的名字的字符串。但无论我如何摆弄它,每当我调用GetVote()时,程序都会崩溃。

我确信我犯了一个或多个非常愚蠢的错误,但我不知道问题出在哪里。任何意见都将不胜感激!

#include <iostream>
#include <vector>
#include <random>
#include <time.h>
using namespace std;
enum gender { male, female };
class Person {
    private:
        string personName;
        gender personGender;
        vector<Person *> voteList;
    public:
        // Constructors
        Person (string, gender);
        // Setters
        void Vote (Person * target) {
            voteList.push_back (target); 
        };
        // Getters
        string GetName () { return personName; };
        string GetVote (int round)
        {
            Person ugh = *(voteList[round]);
            return ugh.GetName ();
        };
};
Person::Person (string a, gender b) {
    personName = a;
    personGender = b; }
void Voting (vector<Person> voters)
{
    for (int i = 0; i < voters.size(); i++) {
        int number = (rand() % voters.size());
        Person * myTarget = &voters[number];
        voters[i].Vote (myTarget);
        cout << voters[i].GetName() << " votes for " << voters[number].GetName() << endl;
    }
    cout << endl;
}
int main()
{
    srand(time(0));
    Person Anna ("Anna", female);
    Person Baxter ("Baxter", male);
    Person Caroline ("Caroline", female);
    Person David ("David", male);
    Person Erin ("Erin", female);
    Person Frank ("Frank", male);
    Person Gemma ("Gemma", female);
    Person Hassan ("Hassan", male);
    vector<Person> theGroup;
    theGroup.push_back (Anna);
    theGroup.push_back (Baxter);
    theGroup.push_back (Caroline);
    theGroup.push_back (David);
    theGroup.push_back (Erin);
    theGroup.push_back (Frank);
    theGroup.push_back (Gemma);
    theGroup.push_back (Hassan);
    for (int n = 0, iterations = (theGroup.size() - 2); n <= iterations; n++)
        Voting (theGroup);
    cout << "ANNA VOTED FOR...";
    for (int n = 0; n <= 5; n++)
    {
        cout << "Round " << (n + 1) << ": " << Anna.GetVote(n) << 'n';
    }
    cin.ignore();
    return 0;
}

当您调用voting时,您传递向量的副本,内容也将被复制。

您应该将此矢量作为参考:

void Voting (vector<Person>& voters) { ... }

您可能还想在GetVote中添加一些安全检查,以确保调用方不会提供超出范围的索引。

首先,您要在各处复制Person对象。例如,将Person对象添加到theGroup向量时,以及再次将同一向量传递到Voting函数时。

复制人在语义上没有任何意义。为了避免这种情况,您应该在Person类中添加一个私有复制构造函数和赋值运算符:

private:
    Person(const Person& other);
    Person& operator=(const Person& rhs);

接下来,您将不得不更改向量以使用Person指针:

    vector<Person *> theGroup;
    theGroup.push_back (&Anna);
    theGroup.push_back (&Baxter);
    theGroup.push_back (&Caroline);
    theGroup.push_back (&David);
    theGroup.push_back (&Erin);
    theGroup.push_back (&Frank);
    theGroup.push_back (&Gemma);
    theGroup.push_back (&Hassan);

您可以使用->运算符在指向对象的指针上调用方法,例如:

    string GetVote (int round)
    {
        Person *ugh = voteList[round];
        return ugh->GetName ();
    };

和:

void Voting (const vector<Person *>& voters)
{
    for (int i = 0; i < voters.size(); i++) {
        int number = (rand() % voters.size());
        Person *myTarget = voters[number];
        voters[i]->Vote (myTarget);
        cout << voters[i]->GetName() << " votes for " << voters[number]->GetName() << endl;
    }
    cout << endl;
}
void Voting (vector<Person> voters)

你想把它作为参考。否则,当你得到voters[number]的地址时,你就得到了函数的一个局部变量的地址——一旦你尝试实际使用它,一切都会变得疯狂

void Voting (vector<Person> &voters)

事实上,这并不完全是问题所在,尽管这仍然与参考文献有关。您通过副本传递原始vector,这意味着原始vector(在main函数中(不会被函数的操作修改(当然,它的内容也是如此(。因此,里面的所有Person都有它们的原始状态,只有一个空的voteList vector。显然,如果你试图取消引用它的任何(不存在的(元素,那就不太好了!