New programmer requires help with linker error.

Hey im stuck with what i think is a linker error i cant seem to get rid of on a uni project any help you guys could give would be appreciated my code follows:

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
48
49
50
    #ifndef PLAYING_CARD_HPP
    #define PLAYING_CARD_HPP

#include <iostream>
     
    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
            };
     
            int _face_value;
            int _suit;
     
     
    public:
            PlayingCard( Suit suit, Face_value face_value ) : _face_value(face_value), _suit(suit) 
            {
            }
     
        	inline int getFaceValue() const {return _face_value;}
           	inline int 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
	friend std::ostream& operator<<(std::ostream &output, const PlayingCard &pc);
    };
     
#endif 


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

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

std::ostream& operator<<(std::ostream &output, const PlayingCard &pc)
{
    output << pc._suit << pc._face_value;
    return output;
}

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

using namespace std;

int main()
{
	
    PlayingCard card1();
    PlayingCard card2();

    if (card1 < card2)
    cout << card1;
    cout << card2;

	
	return 0;
}


The error is this:
1
2
3
4
/tmp/ccVBvwyJ.o: In function `main':
playing_card_test.cpp:(.text+0x5): undefined reference to `card1()'
playing_card_test.cpp:(.text+0xb): undefined reference to `card2()'
collect2: ld returned 1 exit status 


thanks for any assistance you can give.
Last edited on
Here:

1
2
PlayingCard card1();
PlayingCard card2();


you are trying to call functions that do not exist. Create these functions.
Hey thanks for your reply im trying to create 2 new cards and call the function to check one card against another.

Is this anything like what im supposed to be doing from what you suggested

1
2
3
4
5
6
7
8
9
10
11
PlayingCard card1(Suit suit, Face_value face_value)
{
Suit Spades;
Face_value Ace;
}

PlayingCard card2(Suit suit, Face_value face_value)
{
Suit Hearts;
Face_value One;
}


if it is it throws up this error

1
2
3
4
5
6
[playing_card_test.cpp:5:19: error: ‘Suit’ was not declared in this scope
playing_card_test.cpp:5:30: error: ‘Face_value’ was not declared in this scope
playing_card_test.cpp:6:1: error: expected ‘,’ or ‘;’ before ‘{’ token
playing_card_test.cpp:11:19: error: ‘Suit’ was not declared in this scope
playing_card_test.cpp:11:30: error: ‘Face_value’ was not declared in this scope
playing_card_test.cpp:12:1: error: expected ‘,’ or ‘;’ before ‘{’ token


thanks for any further help.


Last edited on
I suggest something like this:

1
2
3
PlayingCard( ) 
{
 }


and create them like this

1
2
PlayingCard card1;
PlayingCard card2;
Last edited on
Ok so my program compiles now even using -ansi and -pedantic but when i try to run the resulting .out file it says command not found rather than outputting card1 and card 2. My code follows:

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

using namespace std;

int main()
{
	
   PlayingCard card1(PlayingCard::Hearts, PlayingCard::Ace);
   PlayingCard card2(PlayingCard::Spades, PlayingCard::King);

    if (card1 < card2)
    cout << card1;
    cout << card2;

	
	return 0;
}


playing_card.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
        #ifndef PLAYING_CARD_HPP
    #define PLAYING_CARD_HPP

#include <iostream>
     
    class PlayingCard {
	private:
    	    int _face_value;
            int _suit;
     
     
    public:       

	enum Face_value {
              Ace,
              Two,
              Three,
              Four,
              Five,
              Six,
              Seven,
              Eight,
              Nine,
              Ten,
              Jack,
              Queen,
              King
            };
     
            enum Suit {
              Clubs,
              Diamonds,
              Hearts,
              Spades
            };
     
            PlayingCard( Suit suit, Face_value face_value ) : _face_value(face_value), _suit(suit) 
            {
            }
     
        	inline int getFaceValue() const {return _face_value;}
           	inline int 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
	friend std::ostream& operator<<(std::ostream &output, const PlayingCard &pc);
    };
     
#endif 


playing_card.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){
	if(_suit == rhs.getSuit())
	return true;
	else if(_suit != rhs.getSuit())
	return false;
	}

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

std::ostream& operator<<(std::ostream &output, const PlayingCard &pc)
{
    output << pc._suit << pc._face_value;
    return output;
}



and the code i used to compile is this
 
g++ playing_card_test.cpp playing_card.hpp playing_card.cpp -ansi -pedantic -o Playing_card_test

i then try to run using
 
Playing_card_test

the error code when trying to run the output file is as follows
 
Playing_card_test: command not found

Thanks for any more help you can supply
Last edited on
Try

./Playing_card_test
Topic archived. No new replies allowed.