Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3443372
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:46:59+00:00 2026-05-18T08:46:59+00:00

If you take a look at my function CardCompare inside of the class… It

  • 0

If you take a look at my function CardCompare inside of the class… It doesn’t work! But, if I instead use the function where it is commented out in Hand.cpp, it works perfectly fine. Why is this?

Also, I am wondering if keeping the CardCompare function in my hand class makes less sense than keeping it in the Card class, if that is possible.

Hand.h

#ifndef HAND_H
#define HAND_H

#include <vector>
#include "Card.h"

class Hand {
    private:
        std::vector<Card> hand;
        int total;
        void CalculateTotal();
        bool CardCompare (Card i, Card j) {return ( i.RankInt() < j.RankInt() ); }//Does not work! :O
    public:
        Hand() {
            total = 0;
        }
        std::vector<Card> GetHand() const{ return hand;};
        void PrintHand();
        void AddToHand(Card c);

};



#endif

Hand.cpp

#include "Hand.h"
#include <iostream>
#include <algorithm>

void Hand::CalculateTotal() {
    for (int i = 0; i < hand.size(); i++) {
            std::cout << hand[i].ToString() << std::endl;
    }
}

void Hand::PrintHand() {
    for (int i = 0; i < hand.size(); i++) {
        std::cout << hand[i].ToString() << std::endl;
    }
    std::cout << std::endl;
}
/* If I place this right here, it works perfect.
bool CardCompare (Card i, Card j) {return ( i.RankInt() < j.RankInt() ); }
*/
void Hand::AddToHand(Card c) {
    hand.push_back(c);
    std::sort(hand.begin(),hand.end(),CardCompare);
}

int main() {
    Hand h;
    h.PrintHand();
    h.AddToHand(Card (2, ( Card::Suit )2 ) );
    h.PrintHand();
    h.AddToHand(Card (3, ( Card::Suit )3 ) );
    h.PrintHand();
    h.PrintHand();
    h.AddToHand(Card (1, ( Card::Suit )2 ) );
    h.PrintHand();
    h.AddToHand(Card (13, ( Card::Suit )3 ) );
    h.PrintHand();

    std::cout<< std::endl << std::endl;

    std::cout << h.GetHand()[0].ToString();
}

Card.h

#ifndef CARD_H
#define CARD_H

#include <string>

class Card {
public:
    enum Suit {
        SUIT_HEART,
        SUIT_DIAMOND,
        SUIT_CLUB,
        SUIT_SPADE
    };
    Card(int r = 1, Suit s = SUIT_HEART) : rank(r), suit(s)
    {}
    int GetRank() const { return rank; };
    Suit GetSuit() const { return suit; };
    std::string ToString() const;
    std::string SuitString() const;
    std::string RankString() const;
    int RankInt() const;


private:
    int rank;
    Suit suit;
    static const char * ranknames[];
    static const char * suitnames[];
    static const int     rankints[];
};
#endif

Card.cpp

#include <iostream>
#include "Card.h"
//#include <vector> //gtfo

const char * Card::ranknames[] = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };
const char * Card::suitnames[] = { "Hearts", "Diamonds", "Clubs", "Spaces" };
const int    Card::rankints[]  = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10 ,10 ,10, 10 };

std::string Card::ToString() const {
    std::string s = RankString();
    s.append(" of ");
    s.append(SuitString());
    return s;
}

std::string Card::SuitString() const {
    return suitnames[suit];
}

std::string Card::RankString() const {
    return ranknames[rank-1];
}

int Card::RankInt() const {
    return rankints[rank-1];
}
 /*
int main() {

    std::vector<Card> Deck;

    for (int i = 0; i < 10 ; i++) {
        Deck.push_back(Card(i+1,(Card::Suit)((i+1)%4)));
        std::cout << Deck[i].ToString() << std::endl;
    }

    std::cout << std::endl << std::endl;
    std::random_shuffle( Deck.begin(), Deck.end() );

    for (int i = 0; i < 10 ; i++) {
            std::cout << Deck[i].ToString() << std::endl;
    }
}*/
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:47:00+00:00Added an answer on May 18, 2026 at 8:47 am

    You are trying to pass a pointer to a member function, so sort can’t use it because it doesn’t have this pointer. In your case you can just change the function to be static:

    static bool CardCompare (Card i, Card j) {return ( i.RankInt() < j.RankInt() ); }
    

    If you do need it be a non-static member function in the future, bind it with boost::bind or std::bind (for C++0x compiler):

    std::sort(hand.begin(),hand.end(),bind(&Hand::CardCompare, this, _1, _2));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Take a look at this example code, which doesn't work: <?xml version='1.0' encoding='UTF-8' ?>
Please take a look at this code: class Foo { public $barInstance; public function
Probably a n00b question but take a look at the following code: function admin_delete
take a look at this code: $(document).ready(function() { document.getElementById(sliderId).onmousedown = sliderMouseDown; }); function sliderMouseDown()
Take a look at this html: <head> <title>Test page</title> <script type=text/javascript> function submitForm() {
Take a look at the image below. I defined the red boxes inside the
Take a look at this code: public class Test { public static void main(String...
Take a look at this table: <table cellpadding=0 cellspacing=0 class=order_form> <tr> <th>Amount</th> <th>Desc</th> <th>Price</th>
Take a look to this function please function CheckHost() { $url = parse_url($_SERVER['HTTP_REFERER']); $host
Take a look here to understand what I want to make: http://jsbin.com/uvoruh $(document).ready(function() {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.