Thread: Vectors

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    51

    Vectors

    I cannot seem to figure out how to pass by reference my vector of pointers. I think I am doing it right and I have tried searching google/ the forums, but I am at a loss.

    Here is my code

    Code:
    	std::vector<baseGameState*> States;
    	States.push_back(new MENU_STATE);
    So that should basically be a vector of pointers to objects that are gamestates if I did that correctly.

    Code:
    			if ((active && States.back()->run(States)) || keys[VK_ESCAPE])	// Active?  Was There A Quit Received?
    			{
    				done=TRUE;										// ESC or DrawGLScene Signalled A Quit
    			}
    In that part of the code I go to the most current vector and run it while passing States. But I am not sure if I am passing it as a reference or is it being copied.(If I try run(&States) it gives me an error message)

    Code:
    	int run(std::vector<baseGameState*>& States){
    		update();
    		render();
    		return 0;
    		};
    And this is what gets the reference I think, But I cannot seem to be able to push anything in the class.

    Can someone help me?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    In that part of the code I go to the most current vector and run it while passing States. But I am not sure if I am passing it as a reference or is it being copied.
    You are passing by reference, assuming that the function definition you show for run() is a member function of baseGameState. However, you do not appear to be actually using the States argument in run().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    51
    States is a private variable used by update to push/pop additional states.

    Code:
    class MENU_STATE: public baseGameState{
    private:											//All of our variables that the update function and render need
    	GLfloat	roll;									// Rolling Texture
    	int selector, max_selector,selected;
    	GLubyte Ycolor,fadeoutcolor;
    	GLfloat yOffset;
    	bool	Boolarrow[4];							//0=up
    													//1=down
    													//2=left
    													//3=right
    	DWORD TimeP[4];									//0=up
    													//1=down
    													//2=left
    													//3=right
    	Texture texture[2];												// Storage For 2 Textures ( NEW )
    	bool colordown;
    	std::vector<baseGameState*> States;
    	bool fadeout,fadeoutdone;
    protected:
    	int render();
    	int update();
    public:
    	MENU_STATE::MENU_STATE();
    	int run(std::vector<baseGameState*>& States){
    		update();
    		render();
    		return 0;
    		};
    	int resize();
    };

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Uhh, so you have a private variable named States and you also make run take a parameter named States? What's the point here?
    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.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    51
    I was trying to make it so that States(private) would get the reference, but I guess that is where my code is screwing up.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You mean that your private States should be a reference bound to some other States somewhere else?
    If so, then you must initialize it, and you must do that in the initializer list that's in the constructors.
    If that's not an option, you'll have to use a pointer.

    I think I'm getting this correctly.
    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.

  7. #7
    Registered User
    Join Date
    Dec 2006
    Posts
    51
    Yea it should be bound to the one in the Main() but I have no idea on how to do it

    edit(Just to clarify): basically I want to be able to modify my original vector from within my classes.
    Last edited by Shadowwoelf; 09-28-2008 at 09:10 AM.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Shadowwoelf View Post
    edit(Just to clarify): basically I want to be able to modify my original vector from within my classes.
    Yes, that's what I thought.
    But have you considered that baseGameState should have its own State vector? After all, the engine should own the states.
    It will also save you headache with pointers and references.
    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.

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    51
    Well I am confused on what you mean by its own vector.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    std::vector<baseGameState*> States;
    Not a reference.
    Not a pointer.
    An instance of a vector which stores baseGameState pointers.

    It's the question of ownership - who owns the game states?
    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.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Posts
    51
    Right now that vector is in the Main().

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, but why is it in main?
    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.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What Elysia is trying to say is:
    Think about where things are, and if you need it somewhere other than where it is visible at the moment, who [1] actually OWNS that data, and how do you get the data to the


    [1] "who" here means a function or object - not a person!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Dec 2006
    Posts
    51
    Well the game I set my gameloop up I need the vector to be in main.

    Code:
    	while(done!=TRUE)												// Loop That Runs While done=FALSE
    	{
    		if (PeekMessage(&msg,NULL,0,0,PM_REMOVE))				// Is There A Message Waiting?
    		{
    			if (msg.message==WM_QUIT)							// Have We Received A Quit Message?
    			{
    				done=TRUE;										// If So done=TRUE
    			}
    			else												// If Not, Deal With Window Messages
    			{
    				TranslateMessage(&msg);							// Translate The Message
    				DispatchMessage(&msg);							// Dispatch The Message
    			}
    		}
    		else													// If There Are No Messages
    		{
    		if(resize==true){
    				States.back()->resize();
    				resize=false;
    			}
    			ElapsedTime = timeGetTime()- LastTime;
    			if(ElapsedTime < DesiredFrameLength)
    			{
    				Sleep(DesiredFrameLength - ElapsedTime);
    				ElapsedTime = DesiredFrameLength;
    			}
    			LastTime = timeGetTime();
    			// Draw The Scene.  Watch For ESC Key And Quit Messages From DrawGLScene()
    			if ((active && States.back()->run(States)) || keys[VK_ESCAPE])	// Active?  Was There A Quit Received?
    			{
    				done=TRUE;										// ESC or DrawGLScene Signalled A Quit
    			}
    			else												// Not Time To Quit, Update Screen
    			{
    				SwapBuffers(hDC);								// Swap Buffers (Double Buffering)
    			}
    This way I can always add any new class onto the vector and not have to change anything in the gameloop.

    edit: Now that I think about it I could make a class to handle the other classes I suppose and use that in my gameloop. Was that what you guys were getting at?
    Last edited by Shadowwoelf; 09-29-2008 at 10:24 AM.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Shadowwoelf View Post
    edit: Now that I think about it I could make a class to handle the other classes I suppose and use that in my gameloop. Was that what you guys were getting at?
    Yes. Something like that.
    You can just create your engine class and call engine->run() and let it do everything.
    Then you only need one copy of the game states, and no reference in the class or main! Brilliant!
    Right?
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM