c++ - 什么是 Unresolved external 因素?

标签 c++

<分区>

我的问题查了没用所以特地来问一下。当我运行我的代码并查看一个零件文件时,它是零件类,有多少零件以及它的成本是多少,它应该告诉我它在文件中的位置以及它是否不是,将其添加到文件中。在教授的帮助下,我纠正了很多错误,但我不断收到 Unresolved external 错误。确切地说是五个。我真的不明白这是什么意思。这是代码和错误:

#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

最佳答案

你已经声明了 swapper

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

但你永远不会定义它。因此,当您的程序调用 swapper 时,编译器希望在链接时提供定义。链接器试图找到符号(即函数),但没有找到。

同样处理其余未声明的函数。要修复它们,您需要编写适当的函数。

关于c++ - 什么是 Unresolved external 因素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20574610/

相关文章:

c++ - 如何在 Windows 上从命令行运行 clang?

c++ - 添加一个 child (引擎Unity like)

c++ - std::is_assignable<A, B>::value 在 A 具有可变参数构造函数时始终为真

c++ - 使用 SURF 和 FLANN 跟踪标记的结果非常糟糕

c++ - 可变参数模板的可扩展性

c++ - iostream GCC 错误,转换为 boost::filesystem::iostream for Windows

基于 C++ rest 的 Web 服务客户端实现

c++ - fread 在新进程中完成时给出 BadPtr

c++ - 使用 C++ 设置 OpenALPR, undefined reference 。

c++ - 如何在运行时 C++ 中包含头文件和函数?