Iterator isuue

Hi everyone,
Does anyone know why I'm having errors on the for() loop I will show you?
I just can't manage to get the code working :'(

1
2
3
4
5
6
7
8
void Player::Displaycards() const
{
    for(std::vector<Card>::iterator it = ownedcards.begin(); it!=ownedcards.end(); ++it)
        {
            std::cout << *it << std::endl;
        }

}


Thanks for every reply I get :)
It would be helpful you posted the actual error, and we may have a to see the definition of Card too.
The error is at least 3 lines long and unfortunately I just can't copy it :(.
It just says more or less: conversion from "__gnu_cxx::__normal_iterator bla bla bla" to non-scalar type "__gnu_cxx::__normal_iterator bla bla bla" requested

And here is Card.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef CARD_H
#define CARD_H

#include <string>
#include <sstream>

class Card
{
public:
    Card(int valueg, int short familyg);
    Card();
    void afficher() const;
    void afficher(std::ostream &flux) const;
    int Value();
    bool caseACE();
    void askACE();

protected:
    short family;
    int value;
    std::string name;
};

#endif // CARD_H 

1
2
3
4
5
6
7
8
9
void Player::Displaycards() const
{
    //for(std::vector<Card>::iterator it = ownedcards.begin(); it!=ownedcards.end(); ++it)
    for(std::vector<Card>::const_iterator it = ownedcards.begin(); it!=ownedcards.end(); ++it)
        {
            std::cout << *it << '\n' ;
        }

}


Better:
1
2
3
4
for( auto it = ownedcards.begin(); it != ownedcards.end(); ++it )
{
    std::cout << *it << '\n' ;
}


Best:
1
2
3
4
for( const Card& c : ownedcards )
{
    std::cout << c << '\n' ;
}
The iterator must be a Constant since the function is, so do this:

1
2
3
4
5
6
7
8
9
void Player::Displaycards() const
{
    vector<Card>::constant_iterator it;
    for( it = ownedcards.begin(); it!=ownedcards.end(); ++it)
        {
            std::cout << *it << std::endl;
        }

}
Ok thanks folks for your replies this fixed my issue :)
And by the wayJlborges I do not know c++11 so I'm looking forward to finding a great tutorial about it
> I do not know c++11 so I'm looking forward to finding a great tutorial about it

Start with the wiki page http://en.wikipedia.org/wiki/C%2B%2B11
and Stroustrup's C++11 FAQ http://www.stroustrup.com/C++11FAQ.html

Then go through Microsoft's Channel 9 video archives of
GoingNative 2012: http://channel9.msdn.com/Events/GoingNative/GoingNative-2012
GoingNative 2013: http://channel9.msdn.com/Events/GoingNative/2013

And you would get a good introduction to C++11.
Topic archived. No new replies allowed.