出现链接器错误
我按照导师制定的指导方针编写了这个程序。当我最终修复了所有的拼写错误和语法错误后,我尝试编译该程序并恢复了 5 个链接器错误。据我所知,该程序绝对没有任何问题,所以我想知道你们中的任何人是否可以为我指出正确的方向。
谢谢
我收到的链接器错误:
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您尚未提供方法的定义:
这就是链接器抱怨的原因。
编辑:要解决此问题,您必须在同一文件中提供这些方法的定义,或者更好的是在 .hpp 文件中提供单独的类声明,并在 .cpp 文件中提供类定义。
You have not provided definitions for methods:
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.
您很可能已经忘记了这些功能的实现。确保您了解这些功能,而不仅仅是声明它们。
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.
编译器抱怨您正在使用一些尚未为其提供代码的函数。
这些函数的定义(而不是前向声明)不存在于您发布的代码中。如果它们存在于其他某个 .cpp 文件中,则必须确保该文件也是项目的一部分(以便编译器和链接器知道编译它并使用在那里找到的代码)。否则,您只需编写实现即可。
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.