获取链接器错误
Getting linker errors
我按照导师的指导原则编写了这个程序。一旦我最终修复了所有的拼写错误和语法错误,我试图编译程序,并恢复了5个链接错误。据我所知,这个程序绝对没有任何问题,所以我想知道你们中是否有人能给我指出正确的方向。谢谢你
链接器错误:
Error 2 error LNK2019: unresolved external symbol "void __cdecl write_records(class SalesRecord *)" (?write_records@@YAXPAVSalesRecord@@@Z) referenced in function _main C:UsersHomeDocumentsVisual Studio 2010ProjectsAssignment10Assignment10Assign10.obj
Error 3 error LNK2019: unresolved external symbol "void __cdecl calc_discounts(class SalesRecord *)" (?calc_discounts@@YAXPAVSalesRecord@@@Z) referenced in function _main C:UsersHomeDocumentsVisual Studio 2010ProjectsAssignment10Assignment10Assign10.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:UsersHomeDocumentsVisual Studio 2010ProjectsAssignment10Assignment10Assign10.obj
Error 5 error LNK1120: 3 unresolved externals C:UsersHomeDocumentsVisual Studio 2010ProjectsAssignment10DebugAssignment10.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 << "nnaNo 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 << "nn***** END OF PROGRAM ******n";
return 0;
}
void open_input(ifstream& input, char name[])
{ int count = 0;
do
{ count++;
if (count != 1)
{ cout << "naInvalid 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 << "naInvalid 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;
}
您还没有提供方法的定义:
write_records
calc_discounts
read_records
这就是链接器抱怨的原因。
编辑:要解决这个问题,要么您必须在同一个文件中为这些方法提供定义,要么更好地将。hpp文件中的类声明和。cpp文件中的类定义分开。
您很可能已经忘记了这些函数的实现。请确保您掌握了这些函数的精髓,而不是仅仅声明它们。
编译器抱怨你使用了一些没有提供代码的函数。
这些函数的定义(不是前向声明)没有出现在您发布的代码中。如果它们存在于其他.cpp文件中,则必须确保该文件也是项目的一部分(以便编译器和链接器知道编译它并使用在其中找到的代码)。否则,您只需要编写实现。
相关文章:
- Netbeans 10:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- CMake 错误:链接器命令失败,退出代码为 1 和 cpp.o 文件
- clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用) - 体系结构的未定义符号 x86_64:
- 如何修复 clang: 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- Android NDK.Build命令失败.未定义的引用.clang++:错误:链接器命令失败,退出代码为1
- 使用cmake错误链接boost日志
- 错误:链接器命令失败,退出代码为 1(使用 -v 查看调用):在 Macbook 上
- C++ XCODE ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -
- clang:错误:链接器命令失败,C++代码中的退出代码为 1(使用 -v 查看调用)
- 静态库中的 g++ 错误链接函数
- 未定义的参考错误链接天然函数Android
- C++编译错误:ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v
- QT Q_PROPERTY错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- ld:找不到体系结构x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
- OSX MOJAVE -LD:架构X86_64 clang找不到符号:错误:链接器命令失败,出口代码1
- C 错误链接器命令失败了出口代码1(使用-V查看调用)
- LD:架构x86_64 clang找不到符号:错误:链接器命令失败,出口代码1(使用-v to See
- 聚输出错误 - 链接列表
- Xcode链接器错误:链接器命令失败,退出代码为1(使用-v查看调用)
- 尝试构建C DLIB示例无法与数百个未定义的参考错误链接