Reputation: 1
I wrote this program using the guidelines set by my instructor. Once I finally fixed all my typos and syntax errors I tried to compile the program and revived 5 linker errors. As far as I can tell there is absolutely nothing wrong with the program, so I was wondering if any of you could point me in the right direction.
Thank you
linker errors I'm receiving :
Error 2 error LNK2019: unresolved external symbol "void __cdecl write_records(class SalesRecord *)" (?write_records@@YAXPAVSalesRecord@@@Z) referenced in function _main C:\Users\Home\Documents\Visual Studio 2010\Projects\Assignment10\Assignment10\Assign10.obj
Error 3 error LNK2019: unresolved external symbol "void __cdecl calc_discounts(class SalesRecord *)" (?calc_discounts@@YAXPAVSalesRecord@@@Z) referenced in function _main C:\Users\Home\Documents\Visual Studio 2010\Projects\Assignment10\Assignment10\Assign10.obj
Error 4 error LNK2019: unresolved external symbol "class SalesRecord * __cdecl read_records(class std::basic_ifstream<char,struct std::char_traits<char> > &)" (?read_records@@YAPAVSalesRecord@@AAV?$basic_ifstream@DU?$char_traits@D@std@@@std@@@Z) referenced in function _main C:\Users\Home\Documents\Visual Studio 2010\Projects\Assignment10\Assignment10\Assign10.obj
Error 5 error LNK1120: 3 unresolved externals C:\Users\Home\Documents\Visual Studio 2010\Projects\Assignment10\Debug\Assignment10.exe 1
//Author William Lovejoy
//Assignment 10
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
const int MAX_FILE_NAME = 35;
const int MAX_ID_LEN = 10;
const int MAX_NAME_LEN = 30;
const double DISCOUNT = 0.10;
const double DISCOUNT_BREAK_POINT = 10;
class SalesRecord;
typedef SalesRecord * SalesRecordPtr;
class SalesRecord
{ private:
char item_id[MAX_ID_LEN + 1];
char item_name[MAX_NAME_LEN + 1];
int quantity_sold;
double regular_price;
double discount_price;
double total_price;
SalesRecord *next;
public:
SalesRecord();
void read(ifstream& in);
void calc_discount_price();
void write(ostream & os) const;
int quantity();
double total_for_item();
bool operator<(const SalesRecord& right) const;
friend SalesRecordPtr read_records (ifstream& in);
friend void calc_discounts(SalesRecordPtr head);
friend void write_records(SalesRecordPtr head);
friend void append(SalesRecordPtr& head, SalesRecord& thisrecord);
friend void delete_records(SalesRecordPtr& head);
};
void open_input(ifstream& input, char name[]);
void open_output(ofstream& output, char name[]);
int main()
{ char again;
int num_records;
char infilename[MAX_FILE_NAME + 1];
ifstream in;
SalesRecordPtr records = NULL;
do
{ open_input(in, infilename);
records = read_records(in);
in.close();
if (records != NULL)
{ calc_discounts(records);
write_records(records);
delete_records(records);
}
else
{ cout << "\n\n\aNo data in file: " << infilename << endl;
}
cout << "\nDo you want to process another file (Y/N)? ";
cin >> again;
cin.ignore(1, '\n');
}
while ( again == 'y' || again == 'Y');
cout << "\n\n***** END OF PROGRAM ******\n";
return 0;
}
void open_input(ifstream& input, char name[])
{ int count = 0;
do
{ count++;
if (count != 1)
{ cout << "\n\aInvalid file name or file does not exist. Please try again."
<< endl;
}
cout << "\nEnter the input file name (maximum of " << MAX_FILE_NAME
<< " characters please)\n:> ";
cin.get(name, MAX_FILE_NAME + 1);
cin.ignore(81, '\n');
input.clear();
} while (input.fail() );
}
void open_output(ofstream& output, char name[])
{ int count = 0;
do
{ count++;
if (count != 1)
{ cout << "\n\aInvalid file name or file does not exist. Please try again."
<< endl;
}
cout << "\nEnter the input file name (maximum of " << MAX_FILE_NAME
<< " characters please)\n:> ";
cin.get(name, MAX_FILE_NAME + 1);
cin.ignore(81, '\n');
output.clear();
output.open(name);
} while (output.fail() );
}
bool SalesRecord::operator<(const SalesRecord& right) const
{ if (_stricmp(item_name, right.item_name) < 0) return true;
else return false;
}
SalesRecord::SalesRecord()
{ next = NULL;
}
void SalesRecord::read(ifstream& in)
{ in.get(item_id, MAX_ID_LEN +1);
while (in.get() != '\n');
in.get(item_name, MAX_NAME_LEN +1);
while (in.get() != '\n');
in >> quantity_sold >> regular_price;
while (in.get() != '\n');
}
void SalesRecord::calc_discount_price()
{ double discount_rate;
if (quantity_sold < DISCOUNT_BREAK_POINT)
discount_rate = 0.0;
else
discount_rate = DISCOUNT;
discount_price = regular_price - (discount_rate * regular_price);
total_price = quantity_sold * discount_price;
}
void SalesRecord::write(ostream & os) const
{ os.setf(ios::fixed); os.setf(ios::showpoint); os.precision(2);
os << item_id << "\n" << item_name << "\n"
<< quantity_sold << " " << discount_price << " "
<< total_price << endl;
}
void append(SalesRecordPtr& head, SalesRecord& thisrecord)
{ SalesRecord * new_record = NULL;
SalesRecord * last = NULL;
new_record = new SalesRecord;
if (new_record == NULL)
{ cout << "\aCan not allocate memory!";
exit(1);
}
*new_record = thisrecord;
new_record->next = NULL;
if (head == NULL)
{ head = new_record;
}
else
{ last = head;
while ( last->next != NULL)
{ last = last->next;
}
last->next = new_record;
}
}
void delete_records(SalesRecordPtr& head)
{ SalesRecord * current = NULL;
SalesRecord * deadmeat = NULL;
current = head;
while (current != NULL)
{ deadmeat = current;
current = current->next;
delete deadmeat;
}
head = NULL;
}
Upvotes: 0
Views: 261
Reputation: 1719
You have not provided definitions for methods:
write_records
calc_discounts
read_records
That is why linker complains.
Edit : To solve the problem, either you have to provide definition for these methods in the same file or even better separate class declarations in a .hpp file and class definitions in a .cpp file.
Upvotes: 1
Reputation: 437376
The compiler is complaining that you are using some functions for which you have not given it the code.
The definitions of these functions (not the forward declarations) are not present in the code you posted. If they exist in some other .cpp file, you have to make sure that file is also part of your project (so the compiler and linker know to compile it and use the code found there). Otherwise, you simply need to write the implementations.
Upvotes: 0
Reputation: 169
More than likely you have forgotten the implementation of those functions. Make sure you have the guts of those functions instead of just declaring them.
Upvotes: 0