Complete beginner in need of urgent help

Hey i have recently started programming at uni and am stuck trying to overload some operators im currently using g++ as my compiler and ubuntu as my os i am getting the error shown below when i try to compile:

1
2
3
4
5
playing_card.cpp playing_card.hpp
playing_card.cpp: In member function ‘bool PlayingCard::operator<(const PlayingCard&)’:
playing_card.cpp:8:25: error: passing ‘const PlayingCard’ as ‘this’ argument of ‘PlayingCard::Suit PlayingCard::getSuit()’ discards qualifiers [-fpermissive]
playing_card.cpp:10:30: error: passing ‘const PlayingCard’ as ‘this’ argument of ‘PlayingCard::Suit PlayingCard::getSuit()’ discards qualifiers [-fpermissive]
playing_card.cpp:12:40: error: passing ‘const PlayingCard’ as ‘this’ argument of ‘PlayingCard::Face_value PlayingCard::getFaceValue()’ discards qualifiers [-fpermissive]

below is my code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef PLAYING_CARD_HPP
#define PLAYING_CARD_HPP

class PlayingCard {
private:
	enum Face_value {
	  Ace,
	  Two,
	  Three,
	  Four,
	  Five,
	  Six,
	  Seven,
	  Eight,
	  Nine,
	  Ten,
	  Jack,
	  Queen,
	  King
	};

	enum Suit {
	  Clubs,
	  Diamonds,
	  Hearts,
	  Spades
	};

	Face_value _face_value;
	Suit _suit;


public:
	PlayingCard( Suit suit, Face_value face_value ) : _face_value(face_value), _suit(suit)
	{
	}

	Face_value getFaceValue() {return _face_value;}
	Suit getSuit() {return _suit;}

 	bool operator< (const PlayingCard& rhs);  //function prototype
        bool operator> (const PlayingCard& rhs);  //function prototype
        bool operator== (const PlayingCard& rhs); //function prototype
        bool operator!= (const PlayingCard& rhs); //function prototype
};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "playing_card.hpp"

using namespace std;

 		// Overloading relational operators
	bool PlayingCard::operator< (const PlayingCard& rhs) {
	if(_suit < rhs.getSuit())
	return true;
	else if(_suit > rhs.getSuit())
	return false;
	return _face_value > rhs.getFaceValue();
	}

I havent started on this section yet but it was required to stop a previous error with the compiler trying to find "main"
1
2
3
4
5
6
7
8
9
#include <iostream>
#include "playing_card.hpp"

using namespace std;

int main()
{
    return 0;
}


Any help you could supply with this problem would be greatly appreciated
Last edited on
You need to declare getFaceValue and getSuit to be const if you want to call them from constants.

1
2
Face_value getFaceValue() const;
Suit getSuit() const;


Why did you include the definitions for these methods and the constructor in the header btw?
I wrote this about a week ago with my lecturer helping me out in a tutorial, to be honest i didnt ask enough questions about what i was doing and so dont understand half of what i've done. Am i declaring as const getFaceValue and getSuit in the public section in my hpp instead of

1
2
Face_value getFaceValue() {return _face_value;}
Suit getSuit() {return _suit;}


or do i declare them at the start of my cpp.

Thanks
First one.
That throws up the error
1
2
3
4
5
/tmp/ccjoQroR.o: In function `PlayingCard::operator<(PlayingCard const&)':
playing_card.cpp:(.text+0x22): undefined reference to `PlayingCard::getSuit() const'
playing_card.cpp:(.text+0x47): undefined reference to `PlayingCard::getSuit() const'
playing_card.cpp:(.text+0x6b): undefined reference to `PlayingCard::getFaceValue() const'
collect2: ld returned 1 exit status
Topic archived. No new replies allowed.