I want to assign a vector to a vector that is returned by a function.
Whenever I run it, I make some Characters and theParty's size is increased. But whenever I try to for loop through party, party's size is 0. Is this line wrong?Code:#include "Character.h" #include <iostream> using namespace std; #include <vector> vector<Character>& makeCharacters() { vector<Character> theParty; bool moreChars, classCorrect; do { string name, clas; cout<<"Enter the name of a character"<<endl; getline(cin, name); if(name.empty()) moreChars = false; else { moreChars = true; do { cout<<"Enter the class of the character"<<endl; getline(cin, clas); if(clas.compare("fighter") != 0 && clas.compare("Fighter") != 0 && clas.compare("mage") != 0 && clas.compare("Mage") != 0 && clas.compare("healer") != 0 && clas.compare("Healer") != 0) { cout<<"Sorry, that is an invalid class choice."<<endl; cout<<"The choices are Fighter, Mage, and Healer."<<endl; classCorrect = false; } else classCorrect = true; } while (!classCorrect); //END DO WHILE FOR CHAR CLASS Character one(name, clas); //MAKE NEW CHARACTER theParty.push_back(one); } //END ELSE } while(moreChars); //END DO WHILE FOR MORE CHAR cout<<"THEPARTYS SIZE: "<<theParty.size()<<endl; return theParty; } //END MAKECHARACTERS() int main() { vector<Character> party; party = makeCharacters(); cout<<"PARTYS SIZE: "<<party.size()<<endl; for(unsigned int i=0;i<party.size();i++) cout<<party.at(i).toString()<<endl; }
I don't understand why party is being initialized with the vector returned by makeCharacters().Code:party = makeCharacters();



LinkBack URL
About LinkBacks



