Thread: Problem with a vector pointer

  1. #1
    jlöhklö
    Guest

    Question Problem with a vector pointer

    To put it brief:


    Code:
    class thisclass
    {
      public:
        vector <someclass> *sc;
        thisclass(vector <someclass> &_sc)
        {
            sc = &_sc;
        }
        void somefunc(const int n)
        {
            int x, int y;
            if((x == someclass[n]->x) && (y == someclass[n]->y))
                 dosomething();
        }
    };
    Get what I'm trying to do here and why it doesn't work (that's the part I do not know)?

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    void somefunc(const int n)
        {
            int x, int y;
            if((x == someclass[n]->x) && (y == someclass[n]->y))
                 dosomething();
        }
    You are comparing the value of x & y with vector elements without setting them to any real value.

    Your compiler might issue a waring, but its something you should really look out for

  3. #3
    jlöhklö
    Guest
    Ah, that's just something I forget to do in the example, but consider as a real comparement in this situation. It should read something like:

    int x = 13, y = 2;

    But what I'm after is how to compare that 'x' element in the someclass-vector.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Ok....how doesnt it work?

    What's the makeup of someclass??

  5. #5
    jlöhklö
    Guest
    Code:
    class Bomb
    {
      public:
    	Bomb(Player &_host, Map &_map, vector <Bomb> &_bombs)
    	{
    		...
    		bombs = &_bombs;
    	}
    	...
    	vector <Bomb> *bombs;
    	void do_flames(const short coord, const short t, const bool xy)
    	{
    		short _x = x, _y = y;
    		bool stop;
    		tile mapt;
    
    		for(int dir = 0;  dir < 2;  dir++)
    			for(int i = coord;;)
    			{
    				...
    				else if(isbomb(mapt))
    					for(size_t n = 0;  n < bombs->size();  n++)
    						if((_x == bombs[n]->x) && (_y == bombs[n]->y))
    						{
    							bombs[n]->timer = 0;
    							break;
    						}
    			}
    	}
    	...
    };
    Results to:

    [code]
    G:\source\bombo>gxx main.cpp -lalleg -Wall
    main.cpp: In member function `void Bomb::do_flames(short int, short int,
    bool)':
    main.cpp:254 (bombs[n]->x): base operand of `->' has non-pointer type `std::vector<Bomb,
    std::allocator<Bomb> >'
    main.cpp:254 (bombs[n]->y): base operand of `->' has non-pointer type `std::vector<Bomb,
    std::allocator<Bomb> >'
    main.cpp:256 (bombs[n]->timer): base operand of `->' has non-pointer type `std::vector<Bomb,
    std::allocator<Bomb> >'
    [/code}

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    vector <someclass> *sc; //??????????????

    Code:
    this is a vector of type someclass called sc
    
    vector <someclass> sc; 
    
    this is a vector of pointer to type someclass called sc
    
    vector <someclass *> sc;
    
    This is an iterator to be used with a vector of type someclass
    
    vector <someclass>::iterator itr;
    
    This is how you read through the vector using the iterator:
    
    for(itr = sc.begin(); itr != sc.end(); ++)
    {
        //x, y, and timer are public member variables in class someclass
        cout << itr->x << ' ' << itr->y << ' ' << itr->timer << endl;
    }
    
    this is how you read through the vector using [] operator
    int index;
    
    for(index = 0; index < sc.size(); ++index)
    {
       cout << sc[index].x << ' ' << sc[index].y << ' ' << sc[index].timer << endl;
    }
    
    this is how you would use [] if sc holds pointers to someclass objects
    
    for(index = 0; index < sc.size(); ++index)
    {
      cout << sc[index]->x << ' ' << sc[index]->y << ' ' << sc[index]->timer << endl;
    }
    
    if you want to use an iterator to a vector of pointers to someclass it would look something like this:
    
    vector <someclass *>::iterator pItr;
    for(pItr = sc.begin(); pItr != sc.end(); ++pItr)
    {
      cout << (*pItr)->x << ' ' << (*pItr)->y << ' ' << (*pItr)->timer << endl;
    }
    Assuming I haven't screwed up along the way.

  7. #7
    jlöhklö
    Guest
    So now I need to do a loop to the constructor like this (sorry, I don't have the time to test it first):
    Code:
    Bomb(vector <Bomb> &_bombs)
    {
        for(size_t i = 0;  i < _bombs.size();  i++)  // I like this rather than those iterators...
            boms[i] = &_bombs[i];
    }
    vector <Bomb *> bombs;
    But what does my first try, vector <Bomb> *bombs, do? Does it take 4 or bombs.size()*4 bytes of memory? Where does it point if I set it to &_bombs?


    Thanks for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM