vector iterator

Hi

I have a question regarding vector iterator

I have a class which looks like this

class CardItem{
public:
CardItem(){
Card newcard(1, JOCKER);
set.push_back(newcard);
Card newcard2(2, DIAMOND);
set.push_back(newcard2);
};
virtual ~CardItem(){};

virtual void Shuffle(int flag);
virtual void Sort(int flag);
protected:
bool fcompare(Card i, Card j);
private:
vector<Card> set;
};

The implementation of the void Sort(...) is

void CardItem::Sort(int flag){
sort (set.begin(), set.end(), fcompare);
}

bool CardItem::fcompare(Card i, Card j){
i.getVal() < j.getVal();
}

The Card class looks like this

class Card{
public:
Card(int val, suit_t suit);
virtual ~Card(){};
int getVal(){return val;};
suit_t getSuit(){return suit;};
private:
int val;
suit_t suit;

};

When I try to compile it says

16 F:\Cygwin\home\Huankai\card_game\cardItem.cpp no matching function
for call to `sort(__gnu_cxx::__normal_iterator<Card*, std::vector<Card,
std::allocator<Card> > >, __gnu_cxx::__normal_iterator<Card*,
std::vector<Card, std::allocator<Card> > >, <unknown type>)'

note f:\Dev-Cpp\include\c++\3.4.2\bits\stl_algo.h:2578 candidates are:
void std::sort(_RandomAccessIterator, _RandomAccessIterator, _Compare)
[with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Card*,
std::vector<Card, std::allocator<Card> > >, _Compare = bool
(CardItem::*)(Card, Card)]

I couldnt understand where I am doing wrong. Doesnt begin() and end() returns random access pointer?
[1973 byte] By [hka26] at [2007-11-20 11:03:00]
# 1 Re: vector iterator
1) Use code tags when posting code. The code you posted is practically unreadable, and many will not help if you don't post your code using the proper code tags. Also arrange your code into one unit.

2) Learn to be const correct in your code.

bool CardItem::fcompare(const Card& i, const Card& j)
{
i.getVal() < j.getVal();
}

And getVal() should be declared as a const function:

class Card
{
public:
Card(int val, suit_t suit);
virtual ~Card(){};
int getVal() const {return val;};
suit_t getSuit() const {return suit;};
private:
int val;
suit_t suit;
};

3) Do not call your variable "set". There is already a std::set class, and it is confusing to me, and maybe the compiler, as your intentions of declaring a variable called "set".

4) Is there a reason why the functions are virtual?

5) The sort predicate function must be a static or non-class function, not a pointer to non-static member function. So fCompare() must be either a static function, or move it outside the class.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-9 1:25:00 >