C++ 什么是未解决的外部问题?
我查了一下我的问题,但没有结果,所以我来这里是专门问的。当我运行我的代码,获取一个部件并查看一个部件文件时,它是部件类,有多少部件,以及它的成本,它应该告诉我它在文件中的位置,如果不是,则将它添加到文件中。在我教授的帮助下,我纠正了很多错误,但我不断得到未解决的外部错误。准确地说是五个。我真的不明白这意味着什么。以下是代码和错误:C++ 什么是未解决的外部问题?,c++,C++,我查了一下我的问题,但没有结果,所以我来这里是专门问的。当我运行我的代码,获取一个部件并查看一个部件文件时,它是部件类,有多少部件,以及它的成本,它应该告诉我它在文件中的位置,如果不是,则将它添加到文件中。在我教授的帮助下,我纠正了很多错误,但我不断得到未解决的外部错误。准确地说是五个。我真的不明白这意味着什么。以下是代码和错误: #include <iostream> #include <fstream> #include <iomanip> #inclu
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
// Sorts vectors (Calls swapper)
void sort(vector<string>& part_number, vector<char>& part_class, vector<int>&ohb, vector<double>& cost);
// Fills vectors
bool get_data (vector <string>& part_number, vector <char>& part_class, vector<int>& part_ohb, vector <double>& part_cost);
// Does a binary search
int bin_search(string key, const vector<string>& part_number);
// Asks user for a part number to search for
void get_target();
// Gets remaining info to add a part number
void get_more_data(char& class_in,int& part_ohb_in,double& part_cost_in);
// Inserts part number data into vectors into the proper location (code for this given below)
void insert_data (vector <string>& part_number, vector <char>& part_class, vector <int>& part_ohb, vector <double>& part_cost, string part_in, char class_in, int part_ohb_in, double part_cost_in);
// Displays info on part number
void display (const vector <string>& part_number, const vector <char>& part_class, const vector <int>& part_ohb, const vector <double>& part_cost, int finder);
// Prints search stats
void print_stats(int searches, int good, int bad);
// Writes out file
void put_data (const vector <string>& part_number, const vector <char>& part_class, const vector <int>& part_ohb, const vector <double>& part_cost);
// Templated swap function. Swaps two items in a vector of any type
template <class CType> void swapper (CType& a, CType & b);
int main() {
vector <string> part_number;
vector <char> part_class;
vector <int> part_ohb;
vector <double> part_cost;
string key, part_in;
int part_ohb_in, searches, good, bad, finder;
char class_in, response;
double part_cost_in;
do {
get_target();
searches++;
get_data (part_number, part_class, part_ohb, part_cost);
sort(part_number, part_class, part_ohb, part_cost);
finder = bin_search(key, part_number);
if (finder == -1) {
cout << part_in << "not found!\n";
get_more_data(class_in, part_ohb_in, part_cost_in);
cout << "Adding part into library...\n";
bad++;
insert_data (part_number, part_class, part_ohb, part_cost, part_in, class_in, part_ohb_in, part_cost_in);
sort(part_number, part_class, part_ohb, part_cost);
}
else {
display (part_number, part_class, part_ohb, part_cost, finder);
good++;
}
cout << "Would you like to search again?" << endl;
} while (toupper(response) != 'Y');
print_stats(searches, good, bad);
put_data (part_number, part_class, part_ohb, part_cost);
return 0;
}
void sort(vector<string>& part_number, vector<char>& part_class, vector<int>& part_ohb, vector<double>& part_cost) {
int i, j, increment;
string temp_pn;
char temp_pc;
int temp_ohb;
double temp_cost;
increment = 3;
while (increment > 0) {
for (i=0; i < part_number.size(); i++) {
j = i;
swapper(temp_pn, part_number[i]);
swapper(temp_pc, part_class[i]);
swapper(temp_ohb, part_ohb[i]);
swapper(temp_cost, part_cost[i]);
while ((j >= increment) && (part_number[j-increment] > temp_pn)) {
swapper(part_number[j], part_number[j - increment]);
swapper(part_class[j], part_class[j - increment]);
swapper(part_ohb[j], part_ohb[j - increment]);
swapper(part_cost[j], part_cost[j - increment]);
j = j - increment;
}
swapper( part_number[j], temp_pn);
swapper( part_class[j], temp_pc);
swapper( part_ohb[j], temp_ohb);
swapper( part_cost[j], temp_cost);
}
if (increment/2 != 0) {
increment = increment/2;
}
else if (increment == 1) {
increment = 0;
}
else {
increment = 1;
}
}
}
bool get_data (vector <string>& part_number, vector <char>& part_class, vector <int>& part_ohb, vector <double>& part_cost) {
bool OK = true;
string partIn;
int ohbIn;
char classIn;
double costIn;
ifstream myFile;
myFile.open("parts.txt");
if (myFile.fail()) {
OK = false;
}
else {
while(myFile >> partIn >> classIn >> ohbIn >> costIn) {
part_number.push_back(partIn);
part_class.push_back(classIn);
part_ohb.push_back(ohbIn);
part_cost.push_back(costIn);
myFile.close();
}
}
return OK;
}
int bin_search(string key, const vector<string>& part_number) {
bool found = false;
int first, mid, last, return_val;
first = 0;
last = part_number.size()-1;
while ((first <= last) && (!found)) {
mid = (first + last) / 2;
if (key == part_number[mid]) {
found = true;
}
else {
if (key < part_number[mid]) {
last = mid - 1;
}
else {
first = mid + 1;
}
}
}
if (found) {
return_val = mid;
}
else {
return_val = -1;
}
return return_val;
}
void get_target(string& key) {
cout << "What part number should I search for?" << endl;
cin >> key;
}
void get_more_data(char& class_in, int& part_ohb_in, double& part_cost_in) {
cout << "Please add in the part class, how many of them there are, and the cost per part: ";
cin >> class_in >> part_ohb_in >> part_cost_in;
}
void insert_data (vector <string>& part_number, vector <char>& part_class, vector <int>& part_ohb, vector <double>& part_cost, string part_in, char class_in, int part_ohb_in, double part_cost_in) {
part_number.push_back(part_in);
part_class.push_back(class_in);
part_ohb.push_back(part_ohb_in);
part_cost.push_back(part_cost_in);
}
void display (const vector <string>& part_number, const vector <char>& part_class, const vector <int>& part_ohb, const vector <double>& part_cost, int finder) {
int total = part_ohb[finder] * part_cost[finder];
cout << "There are " << part_ohb[finder] << " of Part Number " << part_number[finder] << " in inventory. It is a class " << part_class[finder] << " part. The cost is " << part_cost[finder] << ". The value of that inventory is " << total << ".\n";
}
void print_stats(int searches, int good, int bad) {
cout << "You made " << searches << " searches with " << good << " successful searches and " << bad << " bad searches.\n";
}
void put_data (const vector <string>& part_number, const vector <char>& part_class, const vector <int>& part_ohb, const vector <double>& part_cost) {
// for loop for displaying
ofstream myfile;
myfile.open ("parts.txt");
for (int i = 0; i < part_number.size(); i++) {
myfile << part_number[i] << " " << part_class[i] << " " << part_ohb[i] << " " << part_cost[i] << endl;
}
myfile.close();
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
//对向量排序(调用交换程序)
无效排序(向量和零件编号、向量和零件类别、向量和ohb、向量和成本);
//填充向量
bool get_数据(向量和零件编号、向量和零件类别、向量和零件ohb、向量和零件成本);
//进行二进制搜索
int bin_搜索(字符串键、常量向量和零件号);
//要求用户输入要搜索的零件号
void get_target();
//获取剩余信息以添加零件号
无效获取更多数据(字符和类输入、整数和部分ohb输入、双倍和部分成本输入);
//将零件号数据插入向量中的正确位置(代码如下所示)
无效插入数据(矢量和部件号、矢量和部件类、矢量和部件ohb、矢量和部件成本、字符串部件输入、字符类输入、整数部件输入、双部件成本输入);
//显示有关零件号的信息
无效显示(常数向量和零件号、常数向量和零件类、常数向量和零件ohb、常数向量和零件成本、整数查找器);
//打印搜索统计数据
无效打印统计(整数搜索、整数良好、整数不良);
//写出文件
无效输入数据(常数向量和零件号、常数向量和零件类别、常数向量和零件ohb、常数向量和零件成本);
//模板交换函数。交换任意类型向量中的两项
模板无效交换程序(CType&a、CType&b);
int main(){
向量部分_数;
向量部分_类;
向量部分ohb;
向量部分成本;
字符串键,中的部分_;
int part_ohb_in,搜索,好,坏,查找器;
char class_in,响应;
双部分成本;
做{
get_target();
搜索++;
获取零件数据(零件编号、零件类别、零件ohb、零件成本);
排序(零件号、零件类别、零件ohb、零件成本);
查找器=bin_搜索(键、零件号);
如果(查找器==-1){
cout partIn>>classIn>>ohbIn>>costIn){
零件号。推回(零件号);
零件类。推回(classIn);
部分ohb.推回(ohbIn);
零件成本。推回(costIn);
myFile.close();
}
}
返回OK;
}
int bin_搜索(字符串键、常量向量和零件号){
bool-found=false;
int first、mid、last、return\u val;
第一个=0;
last=零件号.size()-1;
而((第一类费用在>>部分ohb费用在>>部分ohb费用在;
}
无效插入数据(矢量和部件号、矢量和部件类、矢量和部件ohb、矢量和部件成本、字符串部件输入、字符类输入、整数部件输入、双部件成本输入){
零件号。推回(零件号);
第二部分:向后推(第二部分);
部分ohb。推回(部分ohb);
零件成本。推回(零件成本);
}
无效显示(常数向量和零件号、常数向量和零件类、常数向量和零件ohb、常数向量和零件成本、整数查找器){
int total=部分ohb[查找器]*部分成本[查找器];
cout Project3Source.obj:错误LNK2019:未解析的外部符号“void\uu cdecl交换程序(char&,char&)”($swapper@D@@YAXAAD0@Z)在函数“void\uu cdecl sort(类std::vector&,类std::vector&,类std::vector&,类std::vector&)中引用(?排序@@YAXAAV$vector@V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@V$allocator@V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@@std@@@2@@@std@@@AAV$vector@DV?$allocator@D@性病@@@2@AAV?$vector@HV?$allocator@H@性病@@@2@AAV?$vector@NV?$allocator@N@std@@@2@@Z)
1> Project3Source.obj:错误LNK2019:未解析的外部符号“void\uu cdecl交换程序(int&,int&)”($swapper@H@@YAXAAH0@Z)在函数“void\uu cdecl sort(类std::vector&,类std::vector&,类std::vector&,类std::vector&)中引用(?排序@@YAXAAV$vector@V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@V$allocator@V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@@std@@@2@@@std@@@AAV$vector@DV?$allocator@D@性病@@@2@AAV?$vector@HV?$allocator@H@性病@@@2@AAV?$vector@NV?$allocator@N@std@@@2@@Z)
1> Project3Source.obj:错误LNK2019:未解析的外部符号“void\uu cdecl交换程序(双&,双&)”($swapper@N@@YAXAAN0@Z)在函数“void\uu cdecl sort(类std::vector&,类std::vector&,类std::vector&,类std::vector&)中引用(?排序@@YAXAAV$vector@V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@V$allocator@V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@@std@@@2@@@std@@@AAV$vector@DV?$allocator@D@性病@@@2@AAV?$vector@HV?$allocator@H@性病@@@2@AAV?$vector@NV?$allocator@N@std@@@2@@Z)
1> C:\Users\Austin Julio\Desktop\CMPSC 121\Projects\Project 3\Debug\Project 3.exe:致命错误LNK1120:5个未解析的外部
未解析的外部是链接器找不到的函数或变量
例如,在您的例子中,您声明并使用了void get_target();
,但是没有定义,只有void get_target(string&key){…}
。您必须声明与定义相同的原型
也许你的意思是:
// declare prototype
void get_target(string& key);
// define function
void get_target(string& key)
{
cout << "What part number should I search for?" << endl;
cin >> key;
}
// use function
std::string key;
get_target(key);
//声明原型
void get_目标(字符串和键);
//定义函数
void get_目标(字符串和键)
{
cout键;
}
//使用函数
std::字符串键;
获取_目标(键);
此外,swapper已声明但未定义(在您发布的代码中)。您已声明
swapper
// templated swap function Swaps two items in a vector of any type
template <class CType>
void swapper (CType& a, CType & b);
//模板交换函数交换任意类型向量中的两个项
模板
无效交换程序(CType&a、CType&b);
但是你从来没有定义过它。所以,当你的程序调用swapper时<
// templated swap function Swaps two items in a vector of any type
template <class CType>
void swapper (CType& a, CType & b);