0

I get the following error in Eclipse when trying to compile (c++)

../CardDeck.cpp:17:22: error: passing ‘const CardDeck’ as ‘this’ argument of ‘int CardDeck::size()’ discards qualifiers [-fpermissive]

if I change int size() method to int size() const the error msg is gone and its compiled. I dont know why ?

the .H file is the following :

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

using namespace std;
class CardDeck{
    private:
        vector<Card*> deck;

    public:

        int size();
        CardDeck();
        CardDeck(const CardDeck& rhs);
        CardDeck& operator=(const CardDeck& rhs);
        Card& draw();
        Card& top();

        bool isEmpty();
        void clear();
        int value();
        CardDeck& operator+=(const CardDeck& rhs); /// not sure if to return ref
        CardDeck& operator+(const CardDeck& rhs);
        friend CardDeck&  operator*(unsigned int num,CardDeck& rhs);
        friend CardDeck&  operator*(CardDeck& lhs,unsigned int num);
        bool operator<=(const CardDeck& rhs );
        bool operator>=(const CardDeck& rhs);
        bool operator<(const CardDeck& rhs);
        bool operator>(const CardDeck& rhs);
        bool operator==(const CardDeck& rhs);
        bool operator!=(const CardDeck& rhs);
        Card*  operator[](int i);
};

and the C++ file is :

#include "CardDeck.h"
int CardDeck::size() {
    return this->deck.size();
}
CardDeck::CardDeck(){};
CardDeck::CardDeck(const CardDeck& rhs){
    this->clear();
    int i;
    for (i=0;i<rhs.size();i++){
        Card* current_card = rhs.deck[i];
        Card* new_copy = new Card(*current_card);
        this->deck.push_back(new_copy);
    }


}
Card* CardDeck::operator[](int i) {
    return this->deck[i];
}



void CardDeck::clear(){
    vector<Card*>::iterator it ;
    for(it=this->deck.begin();it != this->deck.end();++it){
        Card* temp = *it;
        this->deck.erase(it);
        delete(temp);
    }
}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
nadavgam
  • 2,014
  • 5
  • 20
  • 48
  • 1
    change `for (i=0;i – jcxz Mar 01 '16 at 13:03
  • 2
    @jcxz: This only shadows the real problem. – Christian Hackl Mar 01 '16 at 13:08
  • By the way... your `clear` function is needlessly complicated. Just do all the `delete`s first and then call `deck.clear();`. – Christian Hackl Mar 01 '16 at 13:17
  • @ChristianHackl I don't agree. This is just another way to write it. It does not shadow anything. `rhs` is still const, one cannot change it. Although providing an explanation for the error would probably be more beneficial to OP, but that is done in the answer already. – jcxz Mar 01 '16 at 13:19
  • 1
    @jcxz: It shadows the design error of `size()` being non-`const` by preventing the helpful compilation error message. – Christian Hackl Mar 01 '16 at 13:21
  • @ChristianHackl well, if you put it that way, then yes, I agree. However in case OP did not want to provide a const overload (for some reason), then this is the way to go. – jcxz Mar 01 '16 at 13:24

1 Answers1

5

In your copy constructor CardDeck::CardDeck(const CardDeck& rhs), rhs is a reference to a const CardDeck object.

So rhs.size() will not compile unless size() is explicitly marked as being const. That's what your compiler is telling you.

It's good practice to have your code as const-correct as possible as this prevents errant changes to the member data in a class. Really, isEmpty(), and possibly value() should be marked const too, as should all the overloaded relational operators.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483