problem with iteratiors

This is a discussion on problem with iteratiors within the C++ Programming forums, part of the General Programming Boards category; Hi there I'm having trouble with an iterator, I have used them very often so I'm not sure why i'm ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    27

    problem with iteratiors

    Hi there

    I'm having trouble with an iterator, I have used them very often so I'm not sure why i'm getting the error

    heres my code

    Code:
        void food::draw(Canvas &canvas)
        {
                for (Position foodpositions_[5]::iterator i = foodpositions_[0]; i != foodpositions_[5]; ++i)
                    {
                        canvas.blit(foodImage_,0,0,20,20, i->foodpositions_[].x_, i->foodpositions_[].y_);
                    }
        }
    the compiler says;

    Line 81 error: expected initializer before ‘::’ token

    line 81 error: ‘i’ was not declared in this scope

    line 83 error: expected primary-expression before ‘]’ token

    line 83 error: expected primary-expression before ‘]’ token


    the idea behind this code is that it will loop through an array for 5 elements and then blit the contents of each element to the screen.

    Thanks in advance for any help given

    ES

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    27
    What is foodpositions_[5]???
    Your "i" is declared anywhere?
    what is foodpositions_[0]???

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    27
    foodpositions_[ ]is an array of 5 structures that hold two ints.

    its declared elsewhere in the code, heres a looky

    Code:
    //decleration of position
    
    struct Position
    {
        int x_;
        int y_;
        Position(int x,int y) : x_(x), y_(y){}
    };
    Code:
    //declaration within the food class
    Position foodpositions_[5];

    I believe i is declared here;

    Code:
     for (Position foodpositions_[ ]::iterator i = foodpositions_[0]; i != foodpositions_[5]; ++i)
    but like I said, I'm unfamiliar with using iterators, does it need to be declared somewhere else?

  4. #4
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    That isn't an iterator. It's wrong. Maybe look up a C reference on how to implement one.

  5. #5
    Registered User
    Join Date
    Dec 2009
    Posts
    27
    Ok, I've done away with the whole iterator thing and now I haev this

    Code:
        void food::draw(Canvas &canvas)
        {
                for (prog = foodpositions_[prog]; prog != foodpositions_[5]; ++prog)
                    {
                        canvas.blit(foodImage_,0,0,20,20, prog->foodpositions_[prog].x_, prog->foodpositions_[prog].y_);
                    }
        }
    I'm only getting one error now that says prog was not declared in this scope on the line the for loop declared. is there something wrong with my syntax?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Did you declare prog at any point? Using int prog or the like? Not that your for loop will work anyway, as you can't keep both the index and the value from the array in the same variable. Perhaps you just want the completely ordinary and uninteresting
    Code:
    for (int prog = 0; prog < 5; ++prog)
    instead.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    27
    You have come up with this, because you don't know anything about iterators.
    Secondly, your "prog" must be defined somewhere.

    I would say, please refer to your books to fix your problem, and have a better understanding.

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    27
    Ok, I've done some reading, but now I'm not sure if iterators can even be used in an array.

    All the examples I've read only use iterators with lists and vectors from the STL.

    can they be used with an array? if so, I just need to know how to get it started.

    I know you define an iterator for a vector like this;

    Code:
    vector <int>:: iterator iter
    and doing for a list is similar, just using list linstead of vector.

    so can I define an iterator for an array like this

    Code:
    foodpositions_<Position>:: iterator iter

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    27
    Why are you not reading book?
    Your foodpositions is an array of position. It is not a container.
    Read something on STL, you will get some idea about containers, and iterators

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by elsparko View Post
    Ok, I've done some reading, but now I'm not sure if iterators can even be used in an array.

    All the examples I've read only use iterators with lists and vectors from the STL.

    can they be used with an array? if so, I just need to know how to get it started.

    I know you define an iterator for a vector like this;

    Code:
    vector <int>:: iterator iter
    and doing for a list is similar, just using list linstead of vector.

    so can I define an iterator for an array like this

    Code:
    foodpositions_<Position>:: iterator iter
    There are no iterator types for base types. Or to put it another way, the iterator for an array is "pointer to T", which you can use ++ on.

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, 10: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

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