什么是未解决的外部因素?

时间:2013-12-13 19:38:11

标签: c++

我查了一下我的问题无济于事所以我在这里专门问。当我运行我的代码,它需要一部分并查看部件文件时,它是部件类,有多少部件,以及它的成本,它应该告诉我它在文件中的位置以及它是否不是,将它添加到文件中。在我的教授的帮助下,我纠正了很多错误,但我仍然得到了未解决的外部错误。确切地说是五个。我真的不明白这意味着什么。这是代码和错误:

#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)" (?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

2 个答案:

答案 0 :(得分:2)

您已宣布swapper

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

但你永远不会定义它。因此,当您的程序调用{​​{1}}时,编译器期望在链接时提供定义。链接器尝试查找符号(即函数),但找不到它。

与其他未声明的功能相同。要修复它们,您需要编写适当的函数。

答案 1 :(得分: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);

此外,声明了swapper但未定义(在您发布的代码中)。