got error for undefined reference

Apr 24, 2011 at 11:38pm
Hi, I just wrote a program for card war games. but when i try to compile it , it return error for Undefined reference

Here is my code, i got five file for my games.

1.card.h

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#ifndef CARDS_H
#define CARDS_H

#include <iostream>
#include <vector>

using namespace std;

enum suits{diamond, club, heart, spade};

const unsigned int max_ranks = 13;

class Card{
	public:
	//default
	Card();
	//copy constructor
	Card(const Card& orig);
	//explict value con
	Card(suits sv, unsigned int rv);

	~Card();
	//copy assingment oper
	Card& operator = (const Card& rhs);

	suits getSuit() const;
	unsigned int getRank()const;

	void setSuit(unsigned int rv);
	void setRank(suits sv);

	void insert(ostream& out)const;
	
	private:
	suits suit;
	unsigned int rank;
};

class Deck{
	public:
	Deck();
	
	Deck(const Deck& orig);

	~Deck();

	Deck& operator = (const Deck& rhs);

	bool isEmpty() const;

	void shuffle();
	
	Card draw();

	private:
	vector<Card> cards;
};

class Player{
	public:
	
	Player(Deck& aDeck);
	
	~Player();

	Player(const Player& orig);

	Player& operator = (const Player& rhs);

	Card draw();

	void addPoint(int point);

	int point() const;

	void replaceCard(Deck& aDeck);

	private:
	Player();

	private:
	 vector<Card> myPoker;

	int myPoint;
	
	static const int hand_size;
};
#endif 


2. 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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include<iostream>
#include<vector>
#include<algorithm>
#include <cassert>
#include "cards.h"
#include "ramdom.h"

using namespace std;

Card:: Card()
	: rank(1),suit(spade)
{}

Card:: Card(Suits sv,unsigned int rv)
	:rank(rv),suit(sv);
{}

Card:: Card(const Card& orig)
	: rank(orig.rank), suit(orig.suit);
{}

Card::~Card()
{}

Card& Card:: operator = (const Card& rhs){
	if(this != &rhs)
		{
			rank = rhs.rank;
			suit = rhs.suit;
		}
	return *this;
}

unsigned int Card:: getRank() const;{
	return rank;
}


void Card:: setRank(unsigned int rv){
	assert((rv>0)&&(rv<= max_ranks));
	rank = rv;
}

suits Card:: getSuit()const{
	return suit;
}

void card::setSuit(suits sv){
	suit = sv;
}

void Card:: insertCard(ostream& out)const{
	switch(rank)
	{
		case 1: out<<"Ace";break;
		case 11: out<<"Jack";break;
		case 12: out<<"Queen";break;
		case 13: out<<"King"; break;
	default:
	out<<rank; break;
	}

	switch(suit)
	{
		case diamond: out<<"Diamond";break;
		case club: out<<"Clubs";break;
		case heart: out<<"Heart";break;
		case spade: out<<"Spade";break;
	}
}

ostream&operator<<(ostream& out, const Card& aCard)
{
	aCard.insertCard(out);
	return out;
}
//make a deck of cards
Deck::Deck()
{
	for(int i = 1; i<= max_ranks; i++)
	{
		Card c1(diamond,i);
		Card c2(spade,i);
		Card c3(heart,i);
		Card c4(club, i);
		cards.push_back(c1);
		cards.push_back(c2);
		cards.push_back(c3);
		cards.push_back(c4);
	}
}

Deck:: Deck(const Deck& orig){
	operator = (orig);
}
Deck::~Deck(){}

Deck& Deck:: operator(const Deck& rhs)
{
	if(this != &rhs)
	{
		cards = rhs.cards;
	}
	return *this;
}

void Deck:: shuffle(){
	randomInteger randfunc;
	random_shuffle(cards.begin(),cards.end(),randfunc);
}

bool Deck:: isEmpty()const{
	return(cards.size()==0);
}

Card Deck:: draw(){
	assert(!isEmpty());
	Card retval(spade,1);

	if(!isEmpty())
	{
		retval = cards.back();
		cards.pop_back();
	}
	return retval;
}

const Player:: hand_size=3;

Player::Player(Deck& aDeck)
	:myPoint(0);
{
	for(int i = 0; i<hand_size; i++)
	{
		assert(!aDeck.isEmpty());
		myPoker.push_back(aDeck.draw());
	}
}

Player:: Player(const Player& orig)
	:myPoker(orig.myPoker),myPoint(orig.mypoint);
{}

Player::~Player(){}

Player& Player:: operator=(const Player& rhs){
	if(this!= &rhs)
	{
		myPoker = rhs.myPoker;
		myPoint = rhs.myPoint;
	}
	return *this;
}

Card Player:: draw(){
	assert(myPoker.size()>0);
	randomInteger randomizer;
	int removePoker = randomizer(myPoker.size());

	Card draw_card = myPoker[removePoker];
	myPoker[removedPoker] = myPoker.back();
	myPoker.pop_back();
	return draw_card;
}

void Player::addPoint(int x){
	myPoint = myPoint +x;
}

void Player::point() const{
	return myPoint;
}

void Player:: replaceCard(Deck& aDeck){
	assert(!aDeck.isEmpty());
	myPoker.push_back(aDeck.draw());
}


3.random.cpp
1
2
3
4
5
6
7
#include <cstdlib>
#include "random.h"

unsigned int randomInteger::operator()(unsigned int max){
	unsigned int rval = rand();
	return rval % max;
}


4 random.h
1
2
3
4
5
6
7
8
9
10
#ifndef RANDOM_H
#define RANDOM_H

class randomInteger
{
	public:
	unsigned int operator ()(unsigned int max);
};

#endif 


5. my main function
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
#include<iostream>
#include<algorithm>
#include<cassert>
#include<vector>
#include "cards.h"
#include "random.h"

using namespace std;

int main(){
	Deck theDeck;
	theDeck.shuffle();
	
	Player Keith(theDeck);
	Player Zhou(theDeck);

	bool finish = false;

	while(!finish)
	{
		Card card1 = Keith.draw();
	//cout<<"keith"<<card1<<endl;
		//cout<< "Keith's Card"<< card1<<endl;
		Card card2 = Zhou.draw();
		//cout<<"Zhou's card"<< card2 <<endl;

	if(card1.getRank()== card2.getRank()){
		Keith.addPoint(1);
		Zhou.addPoint(1);
		cout<<"game tie"<<endl;
	}
	if (card1.getRank() > card2.getRank())
	{
		Keith.addPoint(2);
		cout<<"keith won the round"<<endl;
	}
	if (card1.getRank() < card2.getRank())
	{
		Zhou.addPoint(2);
		cout<<"Zhou won the round"<<endl;
	}

	finish = theDeck.isEmpty();
	if(!finish) Keith.replaceCard(theDeck);
	finish = theDeck.isEmpty();
	if(!finish) Zhou.replaceCard(theDeck);

	cout<<"Keith's Score"<<Keith.point()<<endl;
	cout<<"Zhou's Score"<<Zhou.point()<<endl;
	}
	return 0;
}


can anyone figure out what's wrong with my code?
Thanks a lot for helping me.
Apr 25, 2011 at 12:05am
Please tell us which reference is undefined; the whole error message will enable us to tell you the problem in seconds.
Apr 25, 2011 at 12:09am
this is what i got for the error.

/tmp/ccmkqtLM.o: In function `main':
war.cpp:(.text+0x1a): undefined reference to `Deck::Deck()'
war.cpp:(.text+0x25): undefined reference to `Deck::shuffle()'
war.cpp:(.text+0x37): undefined reference to `Player::Player(Deck&)'
war.cpp:(.text+0x49): undefined reference to `Player::Player(Deck&)'
war.cpp:(.text+0x64): undefined reference to `Player::draw()'
war.cpp:(.text+0x79): undefined reference to `Player::draw()'
war.cpp:(.text+0x87): undefined reference to `Card::getRank() const'
war.cpp:(.text+0x94): undefined reference to `Card::getRank() const'
war.cpp:(.text+0xb0): undefined reference to `Player::addPoint(int)'
war.cpp:(.text+0xc3): undefined reference to `Player::addPoint(int)'
war.cpp:(.text+0xf2): undefined reference to `Card::getRank() const'
war.cpp:(.text+0xff): undefined reference to `Card::getRank() const'
war.cpp:(.text+0x11b): undefined reference to `Player::addPoint(int)'
war.cpp:(.text+0x14a): undefined reference to `Card::getRank() const'
war.cpp:(.text+0x157): undefined reference to `Card::getRank() const'
war.cpp:(.text+0x173): undefined reference to `Player::addPoint(int)'
war.cpp:(.text+0x1a2): undefined reference to `Deck::isEmpty() const'
war.cpp:(.text+0x1c2): undefined reference to `Player::replaceCard(Deck&)'
war.cpp:(.text+0x1cd): undefined reference to `Deck::isEmpty() const'
war.cpp:(.text+0x1ed): undefined reference to `Player::replaceCard(Deck&)'
war.cpp:(.text+0x1f8): undefined reference to `Player::point() const'
war.cpp:(.text+0x235): undefined reference to `Player::point() const'
war.cpp:(.text+0x278): undefined reference to `Card::~Card()'
war.cpp:(.text+0x289): undefined reference to `Card::~Card()'
war.cpp:(.text+0x29a): undefined reference to `Card::~Card()'
war.cpp:(.text+0x2ab): undefined reference to `Card::~Card()'
war.cpp:(.text+0x2ca): undefined reference to `Player::~Player()'
war.cpp:(.text+0x2db): undefined reference to `Player::~Player()'
war.cpp:(.text+0x2f0): undefined reference to `Player::~Player()'
war.cpp:(.text+0x301): undefined reference to `Player::~Player()'
war.cpp:(.text+0x312): undefined reference to `Deck::~Deck()'
war.cpp:(.text+0x329): undefined reference to `Deck::~Deck()'
collect2: ld returned 1 exit status
Apr 25, 2011 at 12:14am
Firstly, there are a lot of suspicious spaces in your code (e.g. void Card:: setRank)

You are including the headers; are you compiling and linking to card.cpp and random.cpp? The errors you're getting indicate that you're not.
Apr 25, 2011 at 4:58am
thanks. i got it, but different issue occur.
in my card.cpp, the terminal shows that:

Card.cpp:105: error: expected type-specifier before ‘(’ token
Card.cpp:105: error: expected primary-expression before ‘const

what's wrong with my copy assignment operator.
Apr 25, 2011 at 8:54am
From line 98 in your original code, you have this:
1
2
3
4
5
6
7
8
Deck& Deck:: operator(const Deck& rhs)
{
	if(this != &rhs)
	{
		cards = rhs.cards;
	}
	return *this;
}


You haven't specified which operator this is, you have just put operator by itself. is this supposed to be operator = (as in your header file).
Topic archived. No new replies allowed.