【问题标题】:Vector of class type results in Unresolved external symbol error LNK2019类类型的向量导致未解决的外部符号错误 LNK2019
【发布时间】:2014-11-08 05:49:21
【问题描述】:

第一个向量,获胜者向量,抛出一个未解决的外部错误符号错误,我似乎无法确定问题所在。获胜者向量是包含获胜者类对象的向量。当使用整数实现相同的代码时,不会出现任何问题。

两个错误如下: “错误 1 ​​错误 LNK2019:未解析的外部符号“公共:__thiscall Winner::Winner(class std::basic_string,class std::allocator >)”(??0Winner@@QAE@V?$basic_string@DU?$char_traits@ D@std@@V?$allocator@D@2@@std@@@Z) 在函数 _wmain C:\Users\cjbernier\Documents\Classes UML\UML FALL 2014\Computing III (92.201.202)\HW6_Templates_STL 中引用\STLtester\STLtester.obj STLtester"

“错误 2 错误 LNK1120: 1 未解析的外部 C:\Users\cjbernier\Documents\Classes UML\UML FALL 2014\Computing III (92.201.202)\HW6_Templates_STL\STLtester\Debug\STLtester.exe 1 1 STLtester”

// STLtester.cpp : Defines the entry point for the console application.
// This program demostrates 5 different STL containers and uses several

#include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <vector>
#include <list>
#include <stack>
#include <set>
#include <iomanip>
#include <string>
#include <map>

using namespace std;

#pragma once
class Winner {
public:
    Winner(string name = " ");
    void setName(string name);
    const string& getName();
    friend ostream &operator << (ostream &output, const Winner &x);

private:
    string fullName;
};

ostream &operator << (ostream &output, const Winner &x) {
    output << endl << x.fullName;
    return output;
}

int _tmain(int argc, _TCHAR* argv[])
{
        // demonstrating vector template class
        cout << "creating an object of the vector class\n";
        vector<Winner> winnerVector;

        winnerVector.push_back(Winner("George Smith"));

        cout << "the vector contains the following values:\n";
        vector<Winner>::iterator n;
        for (n = winnerVector.begin(); n != winnerVector.end(); n++)
        cout << *n << " ";

        cout << "\n\nadding an element to the vector:\n";
        winnerVector.push_back(Winner("Ron Howard"));

        cout << "the vector now contains the following values:\n";

        for (n = winnerVector.begin(); n != winnerVector.end(); n++)
        cout << *n << " ";

        cout << "\n\nremoving the last element of the vector:\n";
        winnerVector.pop_back();

        cout << "the vector now contains the following values:\n";

        for (n = winnerVector.begin(); n != winnerVector.end(); n++)
        cout << *n << " ";      

    // Demonstrating Vector Template Class  
    cout << "Creating an object of the vector class\n";
    vector<int> intVector;

    cout << "Populating the vector with the integers 0-9\n";
    for (int i = 0; i <= 9; i++) {
        intVector.push_back(i);
    }

    cout << "The vector contains the following values:\n";
    vector<int>::iterator j;
    for (j = intVector.begin(); j != intVector.end(); j++)
        cout << *j << " ";

    cout << "\n\nAdding an element to the vector:\n";
    intVector.push_back(10);

    cout << "The vector now contains the following values:\n";

    for (j = intVector.begin(); j != intVector.end(); j++)
        cout << *j << " ";

    cout << "\n\nRemoving the last 3 elements of the vector:\n";
    intVector._Pop_back_n(3);

    cout << "The vector now contains the following values:\n";

    for (j = intVector.begin(); j != intVector.end(); j++)
        cout << *j << " ";

    // Demonstrating the List Template Class
    cout << "\n\nCreating an object of the list class\n";
    list<char> charList;
    cout << "Populating the list with the letters of the alphabet\n";
    for (char letter = 'A'; letter <= 'Z'; letter++) {
        charList.push_back(letter);
    }

    cout << "The list contains the following values:\n";
    list<char>::iterator k;
    for (k = charList.begin(); k != charList.end(); k++)
        cout << *k << " ";

    cout << "\n\nThe alphabet backwards is:\n";
    list<char>::reverse_iterator rev;
    for (rev = charList.rbegin(); rev != charList.rend(); rev++)
        cout << *rev << " ";

    cout << "\n\nClearing the list:\n";
    charList.clear();
    cout << "The list size is now: " << charList.size();

    // Demonstrating Stack Template Class   
    cout << "\n\nCreating an object of the stack class\n";
    stack<double> doubleStack;

    cout << "Enter a series of double numbers and press enter.\n";
    double nextDouble;
    char nextChar;
    cin.get(nextChar);

    while (nextChar != '\n')
    {
        cin.putback(nextChar);
        cin >> nextDouble;
        doubleStack.push(nextDouble);
        cin.get(nextChar);
    }

    cout << "\nThe double values in reverse order are: \n";
    while (!doubleStack.empty())
    {
        cout << showpoint << setprecision(2) << doubleStack.top() << " ";
        doubleStack.pop();
    }

    // Demonstrating Set Template Class
    cout << "\n\nCreating an object of the set class\n";
    set<string> stringSet;

    stringSet.insert("George Bush");
    stringSet.insert("George Bush");
    stringSet.insert("Fred Johnson");
    stringSet.insert("Chris Lip");

    cout << "The set contains the following Winners:\n";
    set<string>::const_iterator l;
    for (l = stringSet.begin(); l != stringSet.end(); l++)
        cout << "   " << *l << endl;

    cout << "\nSearching for and removing the winner \"Bob Jones\".\n";
    stringSet.erase(stringSet.find("Bob Jones"));

    for (l = stringSet.begin(); l != stringSet.end(); l++)
        cout << "   " << *l << endl;


    // Demonstrating the map class
    cout << "\nCreating an object of the map class\n";
    map<string, string> cars;
    map<string, string>::const_iterator m;

    cout << "Initializing the map:\n";
    cars["Ford"] = "Mustang";
    cars["Honda"] = "Accord";
    cars["Lincoln"] = "MKZ";
    cars["Toyota"] = "Corolla";
    cars["Dodge"] = "Dakota";

    if (cars.empty()) {
        cout << "The map is empty.\n";
    }
    else {
        cout << "The list of cars in the map are:\n";
        for (m = cars.begin(); m != cars.end(); m++)
            cout << m->first << " - " << m->second << endl;
    }

    cout << "\nThe vehicle for Ford is: " << cars["Ford"] << endl << endl;

    if (cars.find("Nissan") == cars.end())
        cout << "Nissan is not in the map.";

    cout << "\n\nThere are vehicles in the map: " << cars.size();

    cout << "\n\nPress any key to exit.";
    cin.get();
    return 0;
}

【问题讨论】:

  • 显示整个错误信息
  • 请从头开始粘贴完整的错误列表...

标签: c++ vector external


【解决方案1】:

我看到以下问题:

  1. 没有Winner::Winner(std::string) 的定义。这解释了链接器错误。事实上,我没有看到Winner 的任何成员函数的定义。
  2. 什么是_Pop_back_n()?它不是我所知道的std::vector 的一部分。它是特定于平台的扩展吗?

【讨论】:

  • 响应 #2,它是 STL Vector 库的一部分。 void _Pop_back_n(size_type _Count) { 指针 _Ptr = this->_Mylast - _Count; _Destroy(_Ptr, this->_Mylast);这->_Mylast = _Ptr; }
猜你喜欢
  • 2014-05-12
  • 2014-05-14
  • 2021-05-06
  • 1970-01-01
  • 2013-06-20
  • 2015-05-18
  • 1970-01-01
  • 2012-10-31
相关资源
最近更新 更多