Unresolved External symbol

I need help with this error message please:

1
2
3
Error	1	error LNK2019: unresolved external symbol "public: __thiscall Book::Book(void)" (??0Book@@QAE@XZ) referenced in function _main	C:\Users\James\documents\visual studio 2013\Projects\Library Assistant\Library Assistant\Library.obj	Library Assistant

}


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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268

#include <iostream>
#include <fstream>
#include <iomanip>
#include "card.h"
#include "book.h"

using namespace std;

void CheckOut(Card c[], Book b[], int, int);
void ShowMenu();
int CreateCard(Card cards[], int numCards);
void PrintAllCards(Card cards[], int numCards);
void CheckIn(Card c[], Book b[], int, int);
void PrintAllBooks(Book books[], int numBooks);

int main()
{
	int numBooks = 0;
	int numCards = 0;
	int i = 0, j = 0;
	int command = 0;

	// declare an array of 20 cards
	Card cards[20];

	// declare an array of 20 books
	Book books[20];

	// declare a file pointer and open the cards file 
	ifstream cardInput("cards.txt");
	if (!cardInput)
	{
		cout << "File cards.txt could not be opened. Fatal error." << endl;
		exit(1);
	}

	cout << "File opened. Reading cards..." << endl;

	// read the next line from the file
	char Name[15];
	char junk[5];
	char phone[15];
	int CardID = 0;
	int temp = 0;

	cardInput.getline(Name, 13, ',');

	// tokenize the line to get the data for class variables

	while (cardInput)
	{
		cardInput.getline(Name, 13, ',');
		cardInput.getline(junk, 5, ',');
		cardInput.getline(phone, 12, ',');
		cardInput.getline(junk, 5, ',');
		cardInput >> CardID >> temp;

		Card working(Name, phone, CardID);
		cards[i] = working;
		i++;

	}
	cardInput.close();

	// declare a file pointer and open the books file 
	ifstream bookInput("books.txt");
	if (!bookInput)
	{
		cout << "File books.txt could not be opened. Fatal error." << endl;
		exit(1);
	}

	cout << "File opened. Reading books..." << endl;

	// read the next line from the file
	char Title[30];
	char Junk[5];
	char Author[30];
	int BookID = 0;
	int Temp = 0;

	bookInput.getline(Title, 13, ',');

	// tokenize the line to get the data for class variables
	
	while (bookInput)
	{
		bookInput.getline(Title, 30, ',');
		bookInput.getline(Junk, 5, ' ');
		bookInput.getline(Author, 10, ',');
		bookInput.getline(Junk, 5, ' ');

		bookInput >> BookID >> Temp;

		Book working(Title, Author, BookID);
		books[i] = working;
		i++;
	}
	bookInput.close();

	// display main menu
	ShowMenu();
	while (command != 0)
	{
		cin >> command;
		// execute the appropriate command
		switch (command){
		case 0:
			break;
		case 1:
			// call function to print all cards
			PrintAllCards(cards, numCards);
			break;
		case 2:
			// call functions to print all books
			PrintAllBooks(books, numBooks);
			break;
		case 3:
			// call to check out book
			CheckOut(cards, books, numCards, numBooks);
			break;
		case 4:
			// call to check in book
			CheckIn(cards, books, numCards, numBooks);
			break;
		case 5:
			// create new library cards
			numCards = CreateCard(cards, numCards);
			break;
		default:
			cout << "INVALID. Menu choices must be btween 0 and 5.";
		}

		ShowMenu();
	}
}

void ShowMenu()
{
	cout << " ------------------------------------- " << endl << endl;
	cout << "1. Show all Library Cards " << endl;
	cout << "2. Show all Books " << endl;
	cout << "3. Check out a Book " << endl;
	cout << "4. Check in a Book " << endl;
	cout << "5. Create a new Library Card " << endl;
	cout << "0. Exit the system " << endl << endl;
}

int CreateCard(Card cards[], int numCards)
{
	{

		char name[30];
		char phone[20];
		int cardID = 0;

		// add 1 to CardID to get ne CardID
		cout << "Enter the Card ID: ";
		cin >> cardID;

		// ask for name
		cout << "Enter your Name: ";
		cin >> name;

		// ask for phone number
		cout << "Enter your Phone Number: ";
		cin >> phone;

		// create a card
		Card temp(name, phone, cardID);

		// copy to arrau
		cards[numCards] = temp;
		numCards++;
		return numCards;
	}

}

void CheckOut(Card cards[], Book books[], int numBooks, int numCards)
{
	int c = 0, b = 0, nCard = 0, nBook = 0;

	cout << "Enter a card to search for: ";
	cin >> nCard;

	while (c < numCards && nCard != cards[c].getCardID())
	{
		c++;
	}
	
	if (c == numCards)
	{
		cout << "Card not Found! Create a new card and try again." << endl;
		return;
	}

	cout << "Card found for ";
	cards[c].printName();
	cout << "!" << endl;

	if (cards[c].getCardID() != 0)
	{
		cout << "HOWEVER... You have not returned your last book." << endl;
		return;
	}

	cout << "Enter the Book ID : ";
	cin >> nBook;

	while (b < numBooks && nBook != books[b].getBookID())
	{
		b++;
	}

	if (b == numBooks)
	{
		cout << "Book Not Found or Already Checked Out!" << endl;
		return;
	}
	
	if (books[b].getCardID() != 0)
	{
		cout << "Book Found! However... Someone has it checked out." << endl;
		return;
	}

	cards[c].setBookID(nBook);
	books[b].setCardID(nCard);

	cout << "Book Checked Out." << endl;
	cards[c].printName();
	
	cout << "has book ";
	books[b].getBookID();
	
	cout << " ";
	books[b].printTitle();
	cout << endl;
	
	return;
}

void CheckIn(Card c[], Book b[], int numBooks, int numCards)
{
	
}

void PrintAllCards(Card cards[], int numCards)
{
	int i = 0;

	for (i = 0; i < numCards; i++)
	{
		cards[i].print();
	}
}

void PrintAllBooks(Book books[], int numBooks)
{
	int j = 0;

	for (j = 0; j < numBooks; j++)
		books[j].print();
}

}



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

#include <iostream>
#include "book.h"

using namespace std;

Book::Book(char t[], char a[], int b)
{
	BookID = b;
	strcpy_s(Title, t);
	strcpy_s(Author, a);
	CheckedOut = false;
	CardID = 0;
}

int Book::getBookID()
{
	return BookID;
}

bool Book::isCheckedOut()
{
	return CheckedOut;
}

void Book::checkOut(int c)
{
	CardID = c;
	CheckedOut = false;
}

void Book::checkIn()
{
	CardID = 0;
	CheckedOut = false;
}

void Book::print()
{

}

void Book::printTitle()
{
	cout << Title;
}

void Book::setCardID(int c)
{

}

int Book::getCardID()
{
	return CardID;
}

}


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 Book_H

#define Book_H


#include <iostream>
#include <iomanip>

class Book{

private:
	int BookID;
	char Title[30];
	char Author[30];
	bool CheckedOut;
	int CardID;

public:
	Book();
	Book(char t[], char a[], int b);
	int getBookID();
	void checkOut(int c);
	void checkIn();
	void print();
	void printTitle();
	bool isCheckedOut();
	int getCardID();
	void setCardID(int c);
};

#endif

}


Any help is appreciated.
Last edited on
closed account (3hM2Nwbp)
A default constructor is declared for the Book class, but never defined. You can either explicitly define it or add the default keyword in its declaration.
Thanks for the reply. How would I go about doing that?
You code has 2 constructors:

This one -

Declaration:
Book(char t[], char a[], int b);

Definition:
1
2
3
4
5
6
7
8
Book::Book(char t[], char a[], int b)
{
	BookID = b;
	strcpy_s(Title, t);
	strcpy_s(Author, a);
	CheckedOut = false;
	CardID = 0;
}


The other one is the default constructor -
Declaration:
Book();

But no definition. You should create a definition for it, it should take no arguments, and preferably initialise all the members to something sensible.

Example-

Definition:
1
2
3
4
5
6
Book()
{
        BookID = -1;
        ....
	CardID = -1; 
}


Try not use magic numbers (30) and prefer std::string to char [] in the first place.
Topic archived. No new replies allowed.