c++ - 我的项目中不断出现 3 个 LNK2019 错误,但不知道为什么

标签 c++

我正在使用 Visual Studios 2012 Ultimate,这是我的错误和主要错误:我所做的唯一编辑是在 linkedbag.h 和 node.h 我分别在头文件的底部 例如:

class blah
{};
#include "blah.cpp"
#endif


//  Created by Tony Chern 9/4/2013
//  Sample code for lab2.cpp

#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include "LinkedBag.h"

using namespace std;

void showMenu();
int checkMenuInput(char[]);
void displayBag(LinkedBag<string>& );

int main()
{
    int choice;
    string str;
    LinkedBag<string> bag;

    cout << "Starting the bag with 6 items." << endl;

    string items[] = {"one", "two", "three", "four", "five", "one"};
    cout << "Add 6 items to the bag: " << endl;
    for (int i = 0; i < 6; i++)
    {
        bag.add(items[i]);
    }  // end for



    do
    {   
        showMenu(); //display menu
        cin >> choice; //store user's choice

        //make sure user types in valid menu choice
        while (choice < 1 || choice > 6) 
        {
            cout << "Please enter a valid menu choice: ";
            cin >> choice;
        }

        if (choice != 6) //if user doesn't want to quit program
        {
            switch (choice)
            {
                case 1: //insert item
                    break;

                case 2: //delete item
                    break;
                case 3: //append item
                    cout << "Item name: ";
                    cin >> str;
                    bag.add(str);
                    break;
                case 4: //print list
                    displayBag(bag);
                    break;
                case 5: //reverse list
                    break;

            }
        }
    } while(choice != 6);
    return 0;
}  // end main


// to display the test menu
void showMenu()
{
    cout << "\n"
         << "1. Insert item\n"
         << "2. Delete item\n"
         << "3. Append item\n"
         << "4. Print list\n"
         << "5. Reverse list\n"
         << "6. Quit\n"
         << "Enter your choice: ";      
}

// to validate the user input to menu.
int checkMenuInput(char input[])
{
    int flag = 0; //0 if valid input, 1 if not

        for (int i = 0; i < strlen(input); i++)
        {
            if((isdigit(input[i]) == 0) && (input[i] != '.'))
            {
                flag = 1;
            }
        }
    return flag;
}

void displayBag(LinkedBag<string>& bag)
{
    cout << "The bag contains " << bag.getCurrentSize()
        << " items:" << endl;
   vector<string> bagItems = bag.toVector();

   int numberOfEntries = (int) bagItems.size();
   for (int i = 0; i < numberOfEntries; i++)
   {
      cout << bagItems[i] << " ";
   }  // end for
    cout << endl;
}  // end displayBag


1>------ Build started: Project: Lab2, Configuration: Debug Win32 ------
1>  Node.cpp
1>  LinkedBag.cpp
1>  Lab2.cpp
1>c:\users\omive_000\documents\visual studio 2012\projects\lab2\lab2\lab2.cpp(90): warning C4018: '<' : signed/unsigned mismatch
1>  Generating Code...
1>Lab2.obj : error LNK2019: unresolved external symbol "public: __thiscall LinkedBag<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::LinkedBag<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(void)" (??0?$LinkedBag@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@QAE@XZ) referenced in function _main

1>Lab2.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall LinkedBag<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::~LinkedBag<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(void)" (??1?$LinkedBag@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@UAE@XZ) referenced in function _main
1>Lab2.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall LinkedBag<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::add(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?add@?$LinkedBag@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@UAE_NABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main
1>C:\Users\omive_000\documents\visual studio 2012\Projects\Lab2\Debug\Lab2.exe : fatal error LNK1120: 3 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

最佳答案

LinkedBag 模板类的实现应该从标题中可见。

参见 this question了解详情。

通过删除对 linkedbag.cpp 的包含,您打破了该条件。话虽这么说,包含 .cpp 文件确实是一件古怪的事情,因此您可能想考虑将实现简单地移动到 header 而不是在此处进行包含。

关于c++ - 我的项目中不断出现 3 个 LNK2019 错误,但不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18710739/

相关文章:

c++ - 使用什么数据结构

C++ 通过引用传递单个元素数组 - 这是事实吗?

c++ - 谷歌浏览器插件

c++ - 数据写入被某些事件中断

c++ - 视差映射中的立体视觉

C++ 接口(interface)实现和子类对象实例化

c++ - OpenSSL 和信号

c++ - 如何最简洁地将变量初始化为0?

c++ - 使用 DirectShow 将字幕文件与 MPEG 视频帧精确同步

C++ 如何修复 typedef 模板和类之间的依赖关系问题