"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 symbolstd::operator<lt;(std::ostream&,std::LinkedList const&('collect2:错误:ld返回1退出状态make:***[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使用类方法的定义,因为编译器知道名称空间std中有一个类LinkedList,所以它可以连接它:

如果add()LinkedList的成员,而LinkedListnamespace std的成员,则完全限定名称必须是::std::LinkedList::add()

但运算符是一个自由函数,因此编译器没有任何内容将其与上一个声明相关联,并且它被放置在全局命名空间中。

解决方案:

  1. 将命名空间更改为不同于std的名称空间
  2. 不要在cpp文件中添加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 {}中只包装您的运算符定义,但如果默认情况下将整个内容放在命名空间中,则更容易避免此类问题。

相关内容

最新更新