Thread: Assignment problem

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    117

    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().

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    NEVER return a reference to a local variable!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It would be a much better idea to take a reference to a vector as a parameter, and just fill that.

  4. #4
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    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
    -- Will you show me how to c++?

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I agree with Daved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  2. Another assignment problem
    By the_winky_files in forum C Programming
    Replies: 14
    Last Post: 10-03-2005, 10:33 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. problem on assignment
    By rodney1001 in forum C Programming
    Replies: 1
    Last Post: 10-29-2001, 03:49 PM
  5. homework assignment problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-29-2001, 10:24 AM