Compile Error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

// DeckOfCards.h

class DeckOfCards {

public:

	DeckOfCards();
	void shuffle();
	void deal();

private:

	int deck[4][13];		
};


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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// DeckOfCards.cpp

#include <iostream>
using std::cout;
using std::left;
using std::right;

#include <iomanip>
using std::setw;

#include <cstdlib>
using std::rand;
using std::srand;

#include <ctime>
using std::time;

#include "DeckOfCards.h"

DeckOfCards::DeckOfCards() {

	
	for(int row = 0; row <= 3; row++) {
		for(int column = 0; column <= 12; column++) {

			deck[row][column] = 0;
		}
	}

	srand(time(0));
}

void DeckOfCards::shuffle() {

	int row;
	int column;

	for(int card = 1; card <= 52; card++) {

		do {

			row = rand() % 4;
			column = rand() %13;
		}while(deck[row][column] != 0);

		deck[row][column] = card;
	}
}

void DeckOfCards::deal() {

	static const char *suit[4] = {"Hearts", "Diamonds", "Clubs", "Spades"};

	static const char *face[13] = {"Ace", "Deuce", "Three", "Four", "Five", "Six",
	"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

	for(int card = 1; card <= 52; card++) {
		for(int row = 0; row <= 3; row++) {
			for(int column = 0; column <= 12; column++) {

				if(deck[row][column] == card) {

					cout << setw(5) << right << face[column] << " of " << setw(8) << left <<
					suit[row] << (card % 2 == 0 ? '\n' : '\t');
				}				
			}
		}
	}
}


1
2
3
4
5
6
7
8
9
10
11
12
13

// exercise.cpp

#include "DeckOfCards.h"

int main() {

	DeckOfCards deckOfCards;

	deckOfCards.shuffle();
	deckOfCards.deal();
	return 0;
}


When i try to compile exercise.cpp i get this error

/tmp/cckBSq95.o: In function `main':
cardShuffle.cpp:(.text+0x14): undefined reference to `DeckOfCards::DeckOfCards()'
cardShuffle.cpp:(.text+0x20): undefined reference to `DeckOfCards::shuffle()'
cardShuffle.cpp:(.text+0x2c): undefined reference to `DeckOfCards::deal()'
collect2: ld returned 1 exit status



When i try to compile DeckOfCards.cpp i get this error

/usr/lib/gcc/i486-linux-gnu/4.4.1/../../../../lib/crt1.o: In function `_start':
/build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.S:115: undefined reference to `main'
collect2: ld returned 1 exit status


Everything is in the same file . What's the problem here
I complied your code verbatim and got this output:

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
Eight of Spades         Three of Diamonds
Queen of Clubs           King of Clubs
 King of Spades          Five of Hearts
 King of Diamonds         Six of Spades
Three of Clubs          Deuce of Spades
Deuce of Diamonds        King of Hearts
 Four of Hearts           Ace of Spades
  Ace of Hearts         Seven of Hearts
  Ten of Diamonds        Nine of Spades
Three of Spades         Deuce of Clubs
 Four of Diamonds       Queen of Spades
 Five of Diamonds         Ace of Clubs
Seven of Spades           Ten of Hearts
  Six of Hearts           Six of Diamonds
 Four of Spades           Six of Clubs
Eight of Clubs           Jack of Clubs
 Five of Clubs           Jack of Hearts
 Four of Clubs            Ace of Diamonds
 Jack of Diamonds        Five of Spades
Deuce of Hearts          Nine of Hearts
 Jack of Spades         Eight of Diamonds
 Nine of Clubs          Queen of Hearts
  Ten of Spades         Seven of Clubs
  Ten of Clubs          Eight of Hearts
 Nine of Diamonds       Three of Hearts
Seven of Diamonds       Queen of Diamonds
Press any key to continue . . .
Either do


g++ -c DeckOfCards.cpp -o DeckOfCards.o
g++ -c main.cpp -o main.o
g++ main.o DeckOfCards.o -o myprogram


or


g++ DeckOfCards.cpp main.cpp -o myprogram


You aren't compiling correctly.
Wow thanks a lot .
Topic archived. No new replies allowed.