0

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

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

1 回答 1

1

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

有关详细信息,请参阅此问题

通过删除包含linkedbag.cpp你打破了这种情况。话虽这么说,包含 .cpp 文件确实是一件很奇怪的事情,因此您可能需要考虑将实现简单地移动到标头而不是在此处执行包含。

于 2013-09-10T09:12:07.627 回答