简体   繁体   English

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

[英]Compiler Error LNK2019 with LinkedList implementation C++

Alright so I'm getting this error with my LinkedList class, but I can't for the life of me figure out where the hell the error is coming from, although clearly I've done something with declaring things. 好的,我的LinkedList类遇到了这个错误,但是我一生都无法弄清楚错误的根源,尽管显然我已经做了一些声明工作。 The error: 错误:

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

Header File: 头文件:

    #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 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 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;
}
friend ostream& operator<<(ostream& out, LinkedList& alist);

is defined in your header, but never actually implemented. 是在标头中定义的,但从未真正实现过。 Implement it and then the linker error should go away. 实施它,然后链接器错误应该消失。

You're trying to cout a struct, and since you haven't actually implemented that operator, the linker can't find it. 您正在尝试cout一个结构,并且由于您实际上尚未实现该运算符,因此链接程序无法找到它。

Similar: http://www.cplusplus.com/forum/windows/37248/ 相似: http : //www.cplusplus.com/forum/windows/37248/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM