简体   繁体   中英

Function with classes and vectors variables in c++

I have a class named Party that includes a private variable named players which is type vector, sting.

class Party
{
    vector <string> players;

    public: 
    Party (string party_name, string boss)  {}; 
    ~Party() {};        

    vector<string> getNames() { return players; };

    void setNames (const vector<string> &new_players) { players=new_players; }

}

I want to write a friend function which will show if the variable P (aslo class with a "Name" variable being private) is party of the Party.

void part_of_party (Party &party, P name)
{   
    bool found=false;

    for (int i=0; ( found==false && i<party.name.size() ); ++i)
    {
        if ( (party.name[i]).compare(name.getName()) == 0)
        {
            found==true;
        };
    }

    if (found==true) { /// }
    else { //// }
}

The compiler doesn't show any errors but no message is printed on the screen (as it supposed to).

Do you have any idea? Thank you.

I'm guessing the problem is this

found==true;

which should be

found=true;

Normally newbies get this wrong the other way around, they use = when they should use == , but you have used == (equality) when you should have used = (assignment). Your compiler should have warned you about this. Pay attention to compiler warnings, it will save you a lot of time and frustration.

Also, not an error, but

if (party.name[i] == name.getName())

is a more obvious way of writing

if ( (party.name[i]).compare(name.getName()) == 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