Error while trying to create a deck of cards

Hello,

I've not long been learning C++ and for an assessment for University, we have to create a deck of cards separated in various files and then create three test files to show our functions working.

Here is my error:

‘std::ostream& PlayingCard::operator<<(std::ostream&, const PlayingCard&)’ must take exactly one argument|
||=== Build finished: 1 errors, 0 warnings ===|

Here is my code:
playingcard.hpp

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
#ifndef PLAYING_CARD_HPP
#define PLAYING_CARD_HPP
#include <iostream>

using namespace std;

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

class PlayingCard {
    public:
        inline int getFaceValue() const { return _face_value; }
        inline Suit getSuit() const { 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

        PlayingCard (int std_face_value = 0, Suit std_suit = Clubs) : _face_value(std_face_value), _suit(std_suit){ }


        friend ostream& operator<< ( ostream& out, const PlayingCard& pc );


    private:
        int _face_value; //variables
        Suit _suit;



};

#endif 


playingcard.cpp

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
#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();
	}

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

	bool PlayingCard::operator== (const PlayingCard& rhs) {
	return _suit == rhs.getSuit() &&_face_value == rhs.getFaceValue();
	}

	bool PlayingCard::operator!= (const PlayingCard& rhs) {
	if(_suit != rhs.getSuit())
	return true;
	return _face_value != rhs.getFaceValue();
	}

	ostream& PlayingCard::operator<<( ostream& out, const PlayingCard& pc ){

        static const string suit_names[4] = {"Clubs", "Diamonds", "Hearts", "Spades"};

        static const string _face_values[13] = {"One", "Two", "Three", "Four", "Five", "Six", "Seven",
        "Eight", "Nine", "Ten", "Jack", "Queen", "King" };

        out << _face_values[pc.getFaceValue()] << suit_names[pc.getSuit()];
        return out;

	}


Excuse me if this is a very simple error (which i'm sure it is), any help is appreciated, thank you. :-)


I think you gave
std::ostream& PlayingCard::operator<<(std::ostream&, const PlayingCard&)
too many arguments or something
Since operator<< is a friend it isn't part of the class and therefore doesn't need PlayingCard:: where it is declared in playingcard.cpp.
Thank you I have now removed PlayingCard:: on the operator<< but I am now getting this error:

undefined reference to `main'

As my main is in a separate file here is the code:

playing_card_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include "playing_card.hpp"

using namespace std;

int main()
{

        PlayingCard card1( 0, Clubs );
        PlayingCard card2( 2, Hearts );

        if( card1 < card2 )
        {
            std::cout << card1 << " is less than " << card2 << endl;
        }
        else
        {
            std::cout << card1 << " is not less than " << card2 << endl;
        }

    return 0;
}


Topic archived. No new replies allowed.