LinkedList 实现 C++ 的编译器错误 LNK2019
Posted
技术标签:
【中文标题】LinkedList 实现 C++ 的编译器错误 LNK2019【英文标题】:Compiler Error LNK2019 with LinkedList implementation C++ 【发布时间】:2015-04-14 04:46:55 【问题描述】:好吧,我的 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】:friend ostream& operator<<(ostream& out, LinkedList& alist);
在您的标头中定义,但从未实际实现。实施它,然后链接器错误应该消失。
您正在尝试cout
一个结构,并且由于您尚未实际实现该运算符,因此链接器找不到它。
类似:http://www.cplusplus.com/forum/windows/37248/
【讨论】:
是的,成功了。类似的链接非常有用,谢谢。以上是关于LinkedList 实现 C++ 的编译器错误 LNK2019的主要内容,如果未能解决你的问题,请参考以下文章
在 C++ 中使用 reduce() 和 std::execution 时出现编译错误