std::vector issues...

This is a discussion on std::vector issues... within the C++ Programming forums, part of the General Programming Boards category; hi! I'm having some problems inserting data into a std::vector. I have a class jogador that has all the variables ...

  1. #1
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194

    std::vector issues...

    hi!

    I'm having some problems inserting data into a std::vector.
    I have a class jogador that has all the variables for the players, and when i insert whose variables into the std::vector, everyone of them except for the ID are saved

    I cant see why this is happening...

    campo.cpp:
    Code:
    //inserts data into vector
    guardaRedes gr(1,0,0,"1");
    gr = c1.insereGR(c1, c1.getEq1().getNome(),c1.getEq1().getNomeMCampo());
    c1.getEq1().insere(&gr); //doesn't insert player ID
    c1.campoJogo[gr.getPosL()][gr.getPosC()]=gr.getID();
    jogador.h
    Code:
    class jogador
    {
    	unsigned int numero;
    	unsigned int posC, posL;
    	string ID; //this variable doesn't get saved
    	bool posseBola;
    	string nomEquipa, nomEquipaAdv;
    
    	public:
    		jogador(int num, unsigned int pc, unsigned int pl, string s):numero(num), posC(pc), posL(pl), ID(s){}
    
    		unsigned int getNumero();
    		void setNumero(unsigned int n);
    		unsigned int getPosC();
    		void setPosC(unsigned int pc);
    		unsigned int getPosL();
    		void setPosL(unsigned int pl);
    		string getID();
    		void setID(string i);
    		string getNomEquipa();
    		void setNomEquipa(string n);
    		string getNomEquipaAdv();
    		void setNomEquipaAdv(string n);
    
    		virtual ponto posicaoVaga(string campJogo[20][37], string nomeCampo)const=0;
    };
    equipa.h
    Code:
    class equipa
    {
    	vector <jogador *> jogadores;
    	
    	string nome;
    	unsigned int numGolosMarc;
    	string nomeEquipAdv;
    	string nomeMeioCampo;
    	
    	public:
    		equipa()
    		{
    		}
    		equipa(string n)
    		{
    			nome = n;
    		}
    
    		void setNomeMCampo(string n);
    		string getNomeMCampo();
    		void setNome(string n);
    		string getNome();
    		void setNomeEqAdv(string n);
    		string getNomeEqAdv();
    		void setNumGolosMarc(unsigned int n);
    		unsigned int getNumGolosMarc();
    
    		void insere(jogador *j); //insert players to std::vector
    		void visualiza() const;
    		jogador * pesquisa(unsigned int n);	
    };
    equipa.cpp
    Code:
    //function that inserts the player into the std::vector
    void equipa::insere(jogador *j)
    {
    	jogadores.push_back(j);
    }
    Is there something wrong with the code? Why doesn't the ID get saved in the std::vector?
    "Artificial Intelligence usually beats natural stupidity."

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't see the connection between the .cpp you posted and the header files?
    gr is a guardaRedes object, but you say your problem is with the jogador class, and based on the function calls, c1 doesn't appear to be any of the classes you've listed.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is entirely possible that you are storing pointers to local objects with a short life-time?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hey!

    campo.cpp is where i insert my players in the field and save there data on the jogadores vector that is on the equipa.h

    I posted these .cpp and the .h files because i think that the linkage between them are responsible for the fact that the ID variable from jogador.h isn't being saved inside the vector jogadores that's in equipa.h

    I really don't understand why i can successfully save all the jogador data inside the vector jogadores and jogador ID (string ID) isn't saved
    They all belong to the same class (class jogador) and are saved the same way.
    Last edited by IndioDoido; 11-24-2007 at 09:07 AM.
    "Artificial Intelligence usually beats natural stupidity."

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I don't see any code where you are allocating a jogador object on the heap. You must do this in order to do what you want. And also in the destructor of the class containing the vector you must call delete on the pointers in the vector or it will leak memory.

    And I wouldn't call the function that does push_back insert because it's not really inserting anything. There is another function that actually does insert into a vector although you should avoid inserting as much as possible with vectors.

  6. #6
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    hi Bubba

    What do you mean by:
    I don't see any code where you are allocating a jogador object on the heap. You must do this in order to do what you want.
    How do i do that?

    About the destructor, are you saying that i have to one for this to work?

    And I wouldn't call the function that does push_back insert because it's not really inserting anything. There is another function that actually does insert into a vector although you should avoid inserting as much as possible with vectors.
    You were right, that function wasn't doing anything but if it wasn't working how were the other jogador variables getting saved on the jogadores vector?

    Is there another way to insert the data i want into the vector? This is a school assignment and i need to use std::vectors
    "Artificial Intelligence usually beats natural stupidity."

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Code:
    Object *pObject = new Object()
    
    MyVector.push_back(pObject);
    Object here is allocated on the heap. When it is added to the vector the object remains intact since it is pointing to an object that still exists.

    Since this is allocated on the heap then it is necessary when cleaning up the vector to call delete for every Object in the vector.

  8. #8
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    do you mean this:
    Code:
    guardaRedes *gr = new guardaRedes();
    gr(1,0,0,"1");
    jogadores.push_back(gr);
    If so, i'm getting these errors
    Code:
    Error	1	error C2512: 'guardaRedes' : no appropriate default constructor available equipa.cpp	62	
    
    Error	2	error C2064: term does not evaluate to a function taking 4 arguments	equipa.cpp	63
    "Artificial Intelligence usually beats natural stupidity."

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    gr(1,0,0,"1");
    Unless you've defined an operator() for this class, that makes no sense.

    I believe this is what you're looking for:
    Code:
    guardaRedes *gr = new guardaRedes(1,0,0,"1");

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    > About the destructor, are you saying that i have to one for this to work?

    Yes. He's saying that, because the vector is a vector of plain pointers, you are now responsible for its memory management.

    For example
    Code:
    #include <iostream>
    #include <vector>
    
    class Object {
    public:
        static std::size_t in_scope;
    
        Object( )  { ++in_scope; }
        ~Object( ) { --in_scope; }
    };
    std::size_t Object::in_scope = 0;
    
    int main( )
    {
        std::vector< Object * > storage;
        storage.reserve( 8 );
    
        while( storage.size() < storage.capacity() ) {
            storage.push_back( new Object );
        }
        // use objects...
    
        // now you are responsible for deleting storage
        // whereas vector would otherwise do this for you
        while( storage.empty() == false ) {
            delete storage.back();
            std::cout << Object::in_scope << '\n';
            storage.pop_back();
        }
    }
    For this reason, vectors of plain pointers are considered especially awful.

  11. #11
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    thanks guys!

    I've done what you said and know its working

    by the way, what is and does: static std::size_t in_scope; ?
    Code:
    class Object {
    public:
        static std::size_t in_scope; //????
    
        Object( )  { ++in_scope; }
        ~Object( ) { --in_scope; }
    };
    std::size_t Object::in_scope = 0; //????
    "Artificial Intelligence usually beats natural stupidity."

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by IndioDoido View Post
    thanks guys!

    I've done what you said and know its working

    by the way, what is and does: static std::size_t in_scope; ?
    Code:
    class Object {
    public:
        static std::size_t in_scope; //????
    
        Object( )  { ++in_scope; }
        ~Object( ) { --in_scope; }
    };
    std::size_t Object::in_scope = 0; //????
    size_t is a typedef for an unsigned int.

  13. #13
    Registered User IndioDoido's Avatar
    Join Date
    Apr 2007
    Posts
    194
    size_t is a typedef for an unsigned int.

    static std::size_t in_scope;
    What is it for? Why do i have to use it?
    "Artificial Intelligence usually beats natural stupidity."

  14. #14
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by cpjust View Post
    size_t is a typedef for an unsigned int.
    Quote Originally Posted by ISO/IEC 9899:TC2 WG14/N1124
    7.17 Common definitions <stddef.h>
    1 The following types and macros are defined in the standard header <stddef.h>. Some
    are also defined in other headers, as noted in their respective subclauses.
    2 The types are
    ptrdiff_t
    which is the signed integer type of the result of subtracting two pointers;
    size_t
    which is the unsigned integer type of the result of the sizeof operator; and
    wchar_t
    which is an integer type whose range of values can represent distinct codes for all
    members of the largest extended character set specified among the supported locales; the
    null character shall have the code value zero.
    so it could be unsigned long int or unsigned long long int or whatever.

    This is from the C99 draft but I think the C++ standard specifies it in a similar manner.
    Last edited by robwhit; 11-25-2007 at 09:20 PM.

  15. #15
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    >What is it for?
    I made in_scope a static member because I thought I had to. In order to keep track of the number of objects in_scope needed to be created before any actual objects, and persist after they were destroyed. That's mostly what static does.

    >Why do i have to use it?
    You don't. I was simply trying to illustrate a point about vectors of pointed-to objects. I hope you got it.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21