1

I have looked up my question to no avail so I am here specifically to ask. When I run my code that takes a part and looks into a file of parts, it's part class, how many of that part there is, and how much it costs, it is supposed to tell me where it is in the file and if it isn't, to add it into the file. With the help of my professor, I corrected a lot of my errors, but I keep getting the unresolved external errors. Five to be exact. I don't really understand what this means. Here is the code and errors:

#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(); } 

Errors:

1>Project3Source.obj : error LNK2019: unresolved external symbol "void __cdecl get_target(void)" (?get_target@@YAXXZ) referenced in function _main 1>Project3Source.obj : error LNK2019: unresolved external symbol "void __cdecl swapper<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (??$swapper@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@YAXAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0@Z) referenced in function "void __cdecl sort(class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &,class std::vector<char,class std::allocator<char> > &,class std::vector<int,class std::allocator<int> > &,class std::vector<double,class std::allocator<double> > &)" (?sort@@YAXAAV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@AAV?$vector@DV?$allocator@D@std@@@2@AAV?$vector@HV?$allocator@H@std@@@2@AAV?$vector@NV?$allocator@N@std@@@2@@Z) 1>Project3Source.obj : error LNK2019: unresolved external symbol "void __cdecl swapper<char>(char &,char &)" (??$swapper@D@@YAXAAD0@Z) referenced in function "void __cdecl sort(class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &,class std::vector<char,class std::allocator<char> > &,class std::vector<int,class std::allocator<int> > &,class std::vector<double,class std::allocator<double> > &)" (?sort@@YAXAAV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@AAV?$vector@DV?$allocator@D@std@@@2@AAV?$vector@HV?$allocator@H@std@@@2@AAV?$vector@NV?$allocator@N@std@@@2@@Z) 1>Project3Source.obj : error LNK2019: unresolved external symbol "void __cdecl swapper<int>(int &,int &)" (??$swapper@H@@YAXAAH0@Z) referenced in function "void __cdecl sort(class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &,class std::vector<char,class std::allocator<char> > &,class std::vector<int,class std::allocator<int> > &,class std::vector<double,class std::allocator<double> > &)" (?sort@@YAXAAV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@AAV?$vector@DV?$allocator@D@std@@@2@AAV?$vector@HV?$allocator@H@std@@@2@AAV?$vector@NV?$allocator@N@std@@@2@@Z) 1>Project3Source.obj : error LNK2019: unresolved external symbol "void __cdecl swapper<double>(double &,double &)" (??$swapper@N@@YAXAAN0@Z) referenced in function "void __cdecl sort(class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > &,class std::vector<char,class std::allocator<char> > &,class std::vector<int,class std::allocator<int> > &,class std::vector<double,class std::allocator<double> > &)" (?sort@@YAXAAV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@AAV?$vector@DV?$allocator@D@std@@@2@AAV?$vector@HV?$allocator@H@std@@@2@AAV?$vector@NV?$allocator@N@std@@@2@@Z) 1>C:\Users\Austin Julio\Desktop\CMPSC 121\Projects\Project 3\Debug\Project 3.exe : fatal error LNK1120: 5 unresolved externals 
7
  • Gotta implement that swapper() in a header file. This is a duplicate, many, many times. Commented Dec 13, 2013 at 19:42
  • Where are the function get_target() taking no arguments and the function template swapper() defined? I can see the declarations but not their definitions. Commented Dec 13, 2013 at 19:43
  • So, what should I do with the swapper() function exactly? Commented Dec 13, 2013 at 20:09
  • @AustinAllenJulio Implement it. It doesn't exist. Commented Dec 13, 2013 at 20:11
  • @molbdnilo where exactly do I implement it? Commented Dec 13, 2013 at 20:16

2 Answers 2

2

You've declared swapper

// templated swap function Swaps two items in a vector of any type template <class CType> void swapper (CType& a, CType & b); 

but you never define it. So, when your program calls swapper, the compiler expects that a definition will be provided at link time. The linker tries to find the symbol (i.e., the function), but doesn't find it.

Same deal with the rest of your undeclared functions. To fix them, you'll need to write the appropriate functions.

Sign up to request clarification or add additional context in comments.

2 Comments

So, I need to write the the definition of the swapper function? OR does it need to go into a template as I've seen on a linked question?
get_target, swapper<string>, swapper<char>, swapper<int> and swapper<double>.
1

Unresolved externals are function or variables the linker can't find.

In your case, for example, you declare and use void get_target(); but there is no definition, just void get_target(string& key) {...}. You have to declare the prototype the same as the definition.

Perhaps you meant this:

// 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); 

Furthermore swapper is declared but not defined (in the code you posted).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.