[英]Error: “Undefined reference to operator<<(std::ostream&, Dogs const&)”
[英]"Undefined reference to 'std::operator<<(std::ostream&, std::LinkedList const&)" C++
大家好,這是一個 uni 項目,目前在編譯期間遇到了這個問題 /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64 -pc-cygwin/bin/ld: LinkedListDemo.o:LinkedListDemo.cpp:(.text+0x178): 對std::operator<<(std::ostream&, std::LinkedList const&)' LinkedListDemo.o:LinkedListDemo.cpp:(.text+0x178): relocation truncated to fit: R_X86_64_PC32 against undefined symbol
未定義引用std::operator<<(std::ostream&, std::LinkedList const&)' LinkedListDemo.o:LinkedListDemo.cpp:(.text+0x178): relocation truncated to fit: R_X86_64_PC32 against undefined symbol
std::operator<<(std::ostream&, std::LinkedList const&)' collect2: 錯誤:ld 返回 1 個退出狀態使: *** [makefile:11: a1] 錯誤 1
#include <iostream>
#include <cstdlib>
#include <string>
#include "LinkedList.h"
using namespace std;
void initialize(LinkedList &l1, LinkedList &l2)
{
l1.add("the black cat was sitting on the black mat that was on the black floor");
l2.add("the dog scared the cat and the cat ran away");
}
int main()
{
LinkedList firstList;
LinkedList secondList;
initialize(firstList, secondList);
cout << "Start lists:" << endl;
cout << "List 1: " << firstList << endl;
//cout << "List 2: " << secondList << endl << endl;
cout << "Concatenating the two lists onto list '1':" << endl;
firstList += secondList;
// cout << "List 1: " << firstList << endl;
//cout << "List 2: " << secondList << endl << endl;
cout << "Removing the word 'was' from list '1':" << endl;
firstList.remove("was");
// cout << "List 1: " << firstList << endl;
//cout << "List 2: " << secondList << endl << endl;
cout << "Removing the word 'away' from list '2':" << endl;
secondList.remove("away");
// cout << "List 1: " << firstList << endl;
//cout << "List 2: " << secondList << endl << endl;
cout << "Removing the word 'cat' from both lists:" << endl;
firstList.remove("cat");
secondList.remove("cat");
//cout << "List 1: " << firstList << endl;
//cout << "List 2: " << secondList << endl << endl;
cout << "Number of occurrences of 'black' in list 1: ";
cout << firstList.count("black") << endl << endl;
// Uncomment this section if you are implementing the extended version of the method remove()
// cout << "Removing 'on the black' from both lists:" << endl;
// firstList.remove("on the black");
// secondList.remove("on the black");
// cout << "List 1: " << firstList << endl;
// cout << "List 2: " << secondList << endl << endl;
cout << "Sorting list 1:" << endl;
firstList.sort();
//cout << firstList << endl << endl;
cout << "The program has finished." << endl;
return 0;
}
這是主文件,旨在工作而不是更改
#ifndef will_PC
#define will_PC
#include <cstdlib>
#include <string>
#include "node.h"
namespace std{
class LinkedList{
public:
node* get_Head() const;
void add(string input);
void remove(string Input);
void sort();
int count(string Input);
string getText();
void operator += (const LinkedList& list);
private:
node* head;
node* tail;
node* n;
};
//this is what outputs the object
std::ostream& operator << ( std::ostream &out, LinkedList const& lst);
}
#endif
#include <iostream>
#include <cstdlib>
#include <string>
#include "LinkedList.h"
using namespace std;
void LinkedList::add(string Input){
tail = NULL;
int count =0;
string word = "";
int len=Input.length();
for (int i=0; i< len;i++)
{
if (Input[i]==' ')
{
n = new node;
n->set_Data(word);
n->set_Previous(tail);
n->set_Next(NULL);
if(tail !=NULL){
tail->set_Next(n);
}
tail = n;
if (count ==0)
{
head = n;
}
word = "";
count +=1;
} else
{
word +=Input[i];
}
}
n = new node;
n->set_Data(word);
n->set_Previous(tail);
n->set_Next(NULL);
tail = n;
}
void LinkedList::remove(string Input){
node* temp;
node* hold;
temp =head;
while (temp!=NULL){
if(Input.compare(temp->get_Data())==0){
hold = temp->get_Next();
temp=temp->get_Previous();
temp->set_Next(hold);
hold->set_Previous(temp);
temp=temp->get_Next();
temp=temp->get_Next();
} else{
temp = temp->get_Next();
}
}
}
int LinkedList::count(string Input){
node* temp;
temp =head;
int count= 0 ;
while (temp != NULL){
if(Input.compare(temp->get_Data())==0){
count +=1;
}
temp = temp->get_Next();
}
return count;
}
void LinkedList::operator += (const LinkedList& list){
node* temp;
cout << list.get_Head()->get_Next()->get_Data()<<endl;
temp = list.get_Head();
while(temp!=NULL){
n=new node;
n->set_Data(temp->get_Data());
n->set_Previous(tail);
n->set_Next(NULL);
tail->set_Next(n);
tail= n;
temp=temp->get_Next();
}
}
void LinkedList::sort(){
node* temp;
temp =head;
while(temp!=NULL){
cout<<temp->get_Data()<<" ";
temp = temp->get_Next();
}
cout<<endl;
}
node* LinkedList::get_Head() const {
return head;
}
std::ostream& operator << ( std::ostream &out, LinkedList const& lst)
{
node* temp = lst.get_Head();
while(temp != NULL){
out<< " "<< temp->get_Data() <<" ";
temp = temp->get_Next();
}
return out;
}
#include <cstdlib>
#include <string>
#include "node.h"
using namespace std;
void node::set_Next(node* nextLink){
next = nextLink;
}
void node::set_Previous(node* previousLink){
previous = previousLink;
}
node* node::get_Next(){
return next;
}
node* node::get_Previous(){
return previous;
}
void node::set_Data(string input){
text = input;
}
string node::get_Data(){
return text;
}
#ifndef Will_Node
#define Will_Node
#include <cstdlib>
#include <string>
namespace std{
class node{
public:
void set_Next(node* nextLink);
void set_Previous(node* previousLink);
void set_Data(string input);
node* get_Next();
node* get_Previous();
string get_Data();
private:
string text;
node* next;
node* previous;
};
}
#endif
非常感謝任何幫助我花了太多時間詛咒試圖解決這個問題
你在命名空間std
聲明你的operator <<
(這是非法的,如注釋中所述),但你在全局命名空間中定義它。
using namespace
與類方法的定義一起工作,因為編譯器知道在 namespace std
中有一個類LinkedList
,所以它可以連接它:
如果
add()
是LinkedList
的成員,並且LinkedList
是namespace std
的成員,則完全限定名稱必須是::std::LinkedList::add()
但是操作符是一個自由函數,所以編譯器沒有將它與之前的聲明聯系起來,它被放置在全局命名空間中。
解決方案:
std
不同的名稱using namespace
,而是將整個內容包裝在namespace{}
:#include <iostream>
#include <cstdlib>
#include <string>
#include "LinkedList.h"
namespace X {
void LinkedList::add(string Input){
// all of the member definitions...
std::ostream& operator << ( std::ostream &out, LinkedList const& lst)
{
node* temp = lst.get_Head();
while(temp != NULL){
out<< " "<< temp->get_Data() <<" ";
temp = temp->get_Next();
}
return out;
}
} // namespace X
//file ends here
您也可以僅將您的運算符定義包裝在namespace {}
,但如果默認情況下將整個內容放在命名空間中,則更容易避免此類問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.