-
Assignment problem
I want to assign a vector to a vector that is returned by a function.
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;
}
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:
party = makeCharacters();
I don't understand why party is being initialized with the vector returned by makeCharacters().
-
NEVER return a reference to a local variable!
-
It would be a much better idea to take a reference to a vector as a parameter, and just fill that.
-
I'm not sure about how to do this with STL but I know you can return static variables because of the static type properties. I have done this before but am not sure about how well this fits in with coding standards. I think that Daved's suggestion is more correct. I would probably call this a hack.
But here is an example of what I mean.
Code:
#define SIZE 16
char *func(void);
int main()
{
char *stuff = malloc(sizeof(char)*SIZE);
stuff = func();
printf("%s", stuff);
}
char *func(void)
{
char *p;
static char thing[SIZE] = "Hello World\n";
p = thing;
return p;
}
Although in c++ you need to use new and delete instead of free and malloc.
Here is a good reference to the static variable...
Static variable - Wikipedia, the free encyclopedia
-
This is BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD!
You are essentially replacing the pointer with allocated memory with the return value of func, resulting in a memory leak!
And you don't even release your malloced memory at the end of main!
You can also get of all this code:
char *p;
static char thing[SIZE] = "Hello World\n";
p = thing;
return p;
...and replace it with...
static char thing[SIZE] = "Hello World\n";
return thing;
Which works the same way, but is shorter.
-