2013-12-13 73 views
1

我查過我的問題無濟於事,所以我在這裏專門問。當我運行我的代碼來獲取部分文件並查看部分文件時,它是部分類,該部分有多少部分,以及需要多少成本,它應該告訴我它在文件中的位置,以及是否它不是,將它添加到文件中。在我的教授的幫助下,我糾正了很多錯誤,但是我一直在收到未解決的外部錯誤。準確的五個。我不太明白這意味着什麼。下面是代碼和錯誤:什麼是未解決的外部?

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

錯誤:

1>Project3Source.obj : error LNK2019: unresolved external symbol "void __cdecl get_target(void)" ([email protected]@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> > &)" ([email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]) 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> > &)" ([email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@[email protected][email protected]@[email protected]@@[email protected][email protected][email protected]@[email protected]@@[email protected][email protected][email protected]@[email protected]@@[email protected]@Z) 
1>Project3Source.obj : error LNK2019: unresolved external symbol "void __cdecl swapper<char>(char &,char &)" ([email protected]@@[email protected]) 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> > &)" ([email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@[email protected][email protected]@[email protected]@@[email protected][email protected][email protected]@[email protected]@@[email protected][email protected][email protected]@[email protected]@@[email protected]@Z) 
1>Project3Source.obj : error LNK2019: unresolved external symbol "void __cdecl swapper<int>(int &,int &)" ([email protected]@@[email protected]) 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> > &)" ([email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@[email protected][email protected]@[email protected]@@[email protected][email protected][email protected]@[email protected]@@[email protected][email protected][email protected]@[email protected]@@[email protected]@Z) 
1>Project3Source.obj : error LNK2019: unresolved external symbol "void __cdecl swapper<double>(double &,double &)" ([email protected]@@[email protected]) 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> > &)" ([email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@[email protected]@[email protected]@[email protected][email protected]@[email protected]@@[email protected][email protected][email protected]@[email protected]@@[email protected][email protected][email protected]@[email protected]@@[email protected]@Z) 
1>C:\Users\Austin Julio\Desktop\CMPSC 121\Projects\Project 3\Debug\Project 3.exe : fatal error LNK1120: 5 unresolved externals 
+0

在頭文件中實現'swapper()'。這是重複的,許多*許多次。 – 2013-12-13 19:42:02

+0

函數'get_target()'不帶參數,函數模板'swapper()'定義在哪裏?我可以看到_declarations_而不是他們的_definitions_。 –

+0

那麼,我應該如何處理swapper()函數呢? – Midge

回答

2

您已經聲明swapper

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

但你永遠不定義它。因此,當您的程序調用swapper時,編譯器期望在鏈接時提供定義。鏈接器試圖找到符號(即函數),但沒有找到它。

與您未申報的其他功能相同。要解決它們,你需要編寫適當的功能。

+0

那麼,我需要寫出swapper函數的定義?或者它是否需要進入模板,就像我在鏈接問題中看到的一樣? – Midge

+0

get_target,swapper ,swapper ,swapper 和swapper 。 –

1

無法解析的外部是函數或變量鏈接器無法找到。

在你的情況,例如,您聲明和使用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); 

而且交換技術的聲明,但沒有定義(在您發佈的代碼)。

相關問題