Problem with vectors!

This is a discussion on Problem with vectors! within the C++ Programming forums, part of the General Programming Boards category; Hello there, fellow programmers! I'm relatively new to programming and while trying to develop my first simple game i've stumbled ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    4

    Problem with vectors!

    Hello there, fellow programmers!

    I'm relatively new to programming and while trying to develop my first simple game i've stumbled upon a problem that i have been unable to solve for days. :*(

    So before throwing myself off a cliff in frustration i turn to you to see if your magic can solve my problem.

    The game involves creating multiple objects of a class ("Enemy") and storing these objects within a vector.

    The problem appears to occure during this storing process.

    I get the following error:

    error C2582: 'operator =' function is unavailable in 'Enemy' .....(directory details)

    This is the .h file of the EventHandler, which is the one handling the storing of this object:
    Code:
    #ifndef INCLUDED_EVENTHANDLER
    #define INCLUDED_EVENTHANDLER
    
    #include "Enemy.h"
    #include <vector>
    
    class EventHandler {
    public:
    	EventHandler();
    	typedef std::vector<Enemy> EnemyVector;
    	EnemyVector enemies;
    	
    	void SpawnEnemy();
    
    };
    
    #endif
    Here is the .cpp file:

    Code:
    #include "EventHandler.h"
    
    
    EventHandler::EventHandler():
    enemies(){}
    
    void EventHandler::SpawnEnemy(){
    
    	enemies.push_back(Enemy());
    }
    If this is not sufficient to be able to solve the problem i'll post more code.

    I'm a biiig noob when it comes to programming (started this summer) so help would be infinitely appriciated!!

    Thanks in advance!
    Last edited by MegaBeast; 07-09-2010 at 04:30 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Considering that the error message is concerned with a member function of the Enemy class, you should post the Enemy class definition.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Ok here is the .h for the Enemy class

    Code:
    #ifndef INCLUDED_ENEMY
    #define INCLUDED_ENEMY
    #include "GameConsole.h"
    
    using namespace std;
    
    class Enemy {
    public:
    	Enemy();
    	double hast;
    	double xPosition;
    	double yPosition;
    	int    Enemy_width;
    	int    Enemy_height;
    	const string EnemyImageFilename;
    	const int    ENEMYIMAGE_X_FRAME_COUNT;
    	const int    ENEMYIMAGE_Y_FRAME_COUNT;
    	GCImage     EnemyImage;
    	~Enemy();
    
    	void render();
    	void move(double execution_time);
    
    };
    
    
    #endif
    And here is the .cpp:

    Code:
    #include "Enemy.h"
    
    
    Enemy::Enemy():
    	hast(200),
    	xPosition(500),
    	yPosition(0),
    	EnemyImageFilename        ("EnemyImage.jpg"),
    	ENEMYIMAGE_X_FRAME_COUNT  (1),
    	ENEMYIMAGE_Y_FRAME_COUNT  (1)
    	
    	{
    		EnemyImage       =         VGCDisplay::openImage(
    			EnemyImageFilename, 
    			ENEMYIMAGE_X_FRAME_COUNT, 
    			ENEMYIMAGE_Y_FRAME_COUNT
    		);
    		Enemy_width = GCDisplay::getWidth(EnemyImage);
    		Enemy_height = GCDisplay::getHeight(EnemyImage);
    
    	}
    
    //movement
    	void Enemy::move(double execution_time){		
    			
    					yPosition+=execution_time*hast;
    	}
    
    	void Enemy::render(){
    
    				GCVector		frameIndex		=	GCVector(0,0);
    				GCVector		position		=	GCVector((int)xPosition,(int)yPosition);
    				GCAdjustment	adjustment		=	GCAdjustment(0.5,0.5);
    				GCDisplay::renderImage(EnemyImage,frameIndex,position,adjustment);
    
    	}
    
    	Enemy::~Enemy(){
    		GCDisplay::closeImage(EnemyImage);
    	}
    Hope that should help!

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I don't think it's a good idea to use a vector of this class. Better use a pointer (maybe a shared pointer). Otherwise Enemy will be copied over and over again, and the image would have to be copied over and over again (unless *that* is changed to a shared pointer).

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Ah. The problem is probably because the Enemy class has non-static const member variables. The compiler was unable to properly generate a copy assignment operator because such copy assignment would logically involve assigning to those member variables, but that cannot be as they are const.

    If they are supposed to be static const member variables, then declare them as such. Otherwise, they probably should be non-const.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    Because you have constants in your class, the compiler was unable to generate a default assignment operator. And vector requires an assignment operator.
    You should think about how you want copies of Enemy to be made and define such an assignment operator.
    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
    Jul 2010
    Posts
    4
    Nice!!

    Changed the consts to non-consts and it worked

    Haven't fully figured out the pointer part yet but i guess its a good idea to do so! :P

    Anyway thanks for superfast answers!!


  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,461
    Good to hear that. Is there any reason why your member variables are public instead of private?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,296
    It looks like your Enemy class does not have well defined copy-construction and assignment functionality. The following code would call closeImage twice on the same image:
    Code:
    {
        Enemy a;
        Enemy b = a;
    }
    As the designer of this class it is your duty to either ensure that these work correctly (however inefficient that may be) or that they are disabled. Otherwise you are likely to run into all kinds of problems later.
    To disable these add the following code to your class:
    Code:
    private:
    Enemy(const Enemy &);
    Enemy& operator=(const Enemy&);
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    4
    Is there any reason why your member variables are public instead of private?
    Not really:P
    I tend to make things as accessible as possible since I'm not yet confident enough to keep things locked up. Simply proof of my newbieness ^^.

    Thank you iMalc!

    Surely a nice addition to the code. Made things a little easier for me ^^. Still learning how to do things properly, so it's a big help!

    Thanks again guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with vectors and classes
    By applenerd in forum C++ Programming
    Replies: 6
    Last Post: 04-08-2006, 06:36 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  3. Vector Problem
    By Morgul in forum Game Programming
    Replies: 16
    Last Post: 02-25-2006, 02:27 PM
  4. Problem with Vectors And Pointers
    By Shamino in forum Game Programming
    Replies: 3
    Last Post: 01-21-2006, 06:23 PM
  5. Shell Sort with Vectors Problem
    By TheSpoiledMilk in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2005, 02:05 PM

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