带有LinkedList实现C ++的编译器错误LNK2019

时间:2015-04-14 04:46:55

标签: c++ visual-studio class linked-list

好吧所以我在我的LinkedList类中遇到了这个错误,但是我不能在我的生活中弄清楚错误来自哪里,虽然我已经做了一些宣告事情。 错误:

Error   1   error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class LinkedList &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAVLinkedList@@@Z) referenced in function _main c:\Users\Cameron\documents\visual studio 2012\Projects\cs260_2\Project6\app.obj Project6

标题文件:

    #ifndef _LINKED_LIST_
#define _LINKED_LIST_

#include <ostream>
using namespace std;

struct Node
{
    char item;
    Node * next;
};
//Node* head;

class LinkedList
{
public:
    Node* head;
    LinkedList();
    LinkedList(ostream& out, LinkedList& alist);
    ~LinkedList();

    void add(char ch);  //check for ch, if it does not exist, add to list
    bool find(char ch); //find and return ch from list
    bool del(char ch);  //find and delete ch from list

    friend ostream& operator<<(ostream& out, LinkedList& alist);
private:
};

#endif // _LINKED_LIST_

LinkedList.cpp

    #include "linkedlist.h"

LinkedList::LinkedList() :
        head(nullptr)
    { }
LinkedList::~LinkedList()
{

}

LinkedList::LinkedList(ostream& out, LinkedList& alist)
{

}

void LinkedList::add(char data)
{
    /*Node* prev = NULL;
    Node* curr = head;

    Node* newNode = new Node;
    newNode->item = data;
    newNode->next = NULL;

    prev = head;
    newNode->next = curr;
    if(prev == NULL)
    {
        head = newNode;
    }
    else
    {
        prev->next = newNode;
    }*/
    Node* temp = new Node;
    temp->item = data;
    temp->next = head;
    head = temp;
}

bool LinkedList::del(char data)
{
    bool returnVal = false;

    if(head == nullptr)
    {
        //return default
    }
    else
    {
        Node* prev = head;
        Node* curr = head->next;
        while(prev->next)
        {
            if(data == curr->item)
            {
                prev->next = curr->next;
                delete curr;
                returnVal = true;
            }
            else
            {
                prev = prev->next;
            }
            curr = curr->next;
         }
    }

     return returnVal;
}

bool LinkedList::find(char data)
{
    bool returnVal = false;
    if(head == nullptr)
    {
        //return default
    }
    else
    {
        Node* prev = head;
        while(prev->next)
        {
            if(data == prev->item)
            {
                returnVal = true;
            }
            prev = prev->next;
        }
     }
    return returnVal;
}

app.cpp

#include <iostream>
#include "linkedlist.h"

void find(LinkedList& list, char ch)
{
    if (list.find(ch))
        std::cout << "found ";
    else
        std::cout << "did not find ";
    std::cout << ch << std::endl;
}

int main()
{
    LinkedList  list;

    list.add('x');
    list.add('y');
    list.add('z');
    std::cout << list;
    find(list, 'y');

    list.del('y');
    std::cout << list;
    find(list, 'y');

    list.del('x');
    std::cout << list;
    find(list, 'y');

    list.del('z');
    std::cout << list;
    find(list, 'y');

    return 0;
}

1 个答案:

答案 0 :(得分:0)

friend ostream& operator<<(ostream& out, LinkedList& alist);

在标头中定义,但从未实际实现过。实现它然后链接器错误应该消失。

您正在尝试cout结构,并且由于您实际上没有实现该运算符,链接器无法找到它。

类似:http://www.cplusplus.com/forum/windows/37248/