Why won't this compile?

Apr 24, 2013 at 11:16pm
I am using Visual studio 2010 to write my C++ code. The syntax of this code is right (I think) because the editor did not raise any flags but when I try to compile it it returns the following error:

Final_Project.obj : error LNK2019: unresolved external symbol "int __cdecl readBooks(class std::basic_ifstream<char,struct std::char_traits<char> > &,struct bookType)" (?readBooks@@YAHAAV?$basic_ifstream@DU?$char_traits@D@std@@@std@@UbookType@@@Z) referenced in function _main
1>C:\Users\Brian\Desktop\Final Project (C++)\Final_Project\Debug\Final_Project.exe : fatal error LNK1120: 1 unresolved externals

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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

ifstream infile;
ofstream outfile;

//struct
struct bookType
{
string itemName;
string listingID;
string sellerSku;
double price;
int quantity;
string openDate;
string itemNote;
int itemCondition;
string productID;
string market;
};

// Function dec
int readBooks(ifstream& infile, bookType);

// Main
int main()
{
	bookType goodBooks[100]; // array of books
	int count = 0;
	int index = 0;

	infile.open("books.txt"); // open input file
		if (!infile)
		{
			cout << "Cannot open the input file. Program terminates!"<< endl;
		}

count = readBooks(infile, goodBooks[100]);

for(index = 0; index < count; index++)
{
cout << goodBooks[index].itemName << "  " << goodBooks[index].listingID << "  " << goodBooks[index].sellerSku <<"  "<<endl;
cout << goodBooks[index].price << "  " << goodBooks[index].quantity << "  " << goodBooks[index].openDate << "  " 
	 << goodBooks[index].price << "  "<<endl;
cout << goodBooks[index].itemNote <<endl;
cout << goodBooks[index].itemCondition << "  "<< goodBooks[index].productID << "  "<< goodBooks[index].market<<endl;
}

system ("PAUSE");
return 0;
}

int readBooks(ifstream& infile, bookType goodBooks[100]) // Function to read in the external data from the file
{
char ignore;
int index = 0;
int count = 0;

while (infile)
    {
		getline (infile, goodBooks[index].itemName, '\t');
		infile >>  goodBooks[index].listingID, '\t';
		infile >> goodBooks[index].sellerSku, '\t';
		infile >> goodBooks[index].price, '\t';
		infile >> goodBooks[index].quantity, '\t';
		getline (infile, goodBooks[index].openDate, '\t');
		getline (infile, goodBooks[index].itemNote, '\t');
		infile >>  goodBooks[index].itemCondition, '\t';
		infile >>  goodBooks[index].productID, '\t';
		infile >>  goodBooks[index].market, '\t';
		infile.get(ignore);

		count = index++;
	}
return(count);
}
Apr 24, 2013 at 11:19pm
The error message says you have not defined the function int readBooks(ifstream& infile, bookType);. Note that the parameter type does not match the readBooks function that you have defined.
Apr 24, 2013 at 11:23pm
closed account (Dy7SLyTq)
what r u talking about peter it matches

[edit]
sorry peter your right i forgot it needed to be a pointer
Last edited on Apr 25, 2013 at 8:12pm
Apr 24, 2013 at 11:45pm
The prototype (line 26) should be
int readBooks(ifstream& infile, bookType *);

and the function call (line 41)
count = readBooks(infile, goodBooks);
Apr 24, 2013 at 11:52pm
Thank you all for your answers. Chevil, what you sugested worked. Thank you.

What does the * do to bookType?
Last edited on Apr 24, 2013 at 11:55pm
Apr 25, 2013 at 12:01am
It's one way declaring that an array is to be passed to the function.
It might be more legible to instead put
int readBooks(ifstream& infile, bookType[]);
Topic archived. No new replies allowed.