LinkedList 实现 C++ 的编译器错误 LNK2019
Compiler Error LNK2019 with LinkedList implementation C++
好吧,我的 LinkedList class 出现了这个错误,但我终究无法弄清楚错误到底是从哪里来的,尽管很明显我已经做了一些事情与声明的事情。
错误:
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;
}
friend ostream& operator<<(ostream& out, LinkedList& alist);
在您的 header 中定义,但从未真正实现。实施它,然后链接器错误应该消失。
您正在尝试 cout
一个结构,但由于您尚未实际实现该运算符,因此链接器无法找到它。
好吧,我的 LinkedList class 出现了这个错误,但我终究无法弄清楚错误到底是从哪里来的,尽管很明显我已经做了一些事情与声明的事情。 错误:
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;
}
friend ostream& operator<<(ostream& out, LinkedList& alist);
在您的 header 中定义,但从未真正实现。实施它,然后链接器错误应该消失。
您正在尝试 cout
一个结构,但由于您尚未实际实现该运算符,因此链接器无法找到它。