我一直在我的项目中得到3个LNK2019错误,并且不知道为什么

时间:2013-09-10 04:28:31

标签: c++

我正在使用Visual Studios 2012 Ultimate,这是我的错误和主要:我所做的唯一编辑是在linkedbag.h和node.h中我分别在#include linkedbag.cpp和node.cpp中找到了头文件的底部Ex:

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 ==========

1 个答案:

答案 0 :(得分:1)

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

有关详细信息,请参阅this question

删除包含到linkedbag.cpp后,您就破坏了这种情况。话虽这么说,包括.cpp文件确实是一件古怪的事情,所以你可能想要考虑简单地将实现移到标题而不是在这里做包含。