我花了很多时间来修复此错误:
Undefined symbols for architecture x86_64:
"operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, SingleLinkedLists<int>&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我不确定问题是什么。我发现人们与Xcode有类似的问题,我试图按照他们的步骤,但似乎没有任何工作。我刚刚更新了Xcode,但它也没有解决我的问题。这是我的代码:
#ifndef SingleLinkedLists_h
#define SingleLinkedLists_h
#include <iostream>
template <class T>
class SingleLinkedLists {
private:
struct Node {
T data;
Node* next;
};
Node* head;
Node* tail;
public:
// Constructors
SingleLinkedLists() : head(nullptr), tail(nullptr) {}
SingleLinkedLists(SingleLinkedLists const& value);
~SingleLinkedLists();
// Overloaded operators
SingleLinkedLists& operator=(SingleLinkedLists const& rhs);
friend std::ostream& operator<<(std::ostream& str, SingleLinkedLists& data);
// Operators in Single Linked List
void swap(SingleLinkedLists& other) noexcept;
void createNode(const T& theData);
void createNode(T&& theData);
void display() const;
void insertHead(const T& theData);
void insertPosition(int pos, const T& theData);
void deleteHead();
void deleteTail();
void deletePosition(int pos);
bool search(const T& x);
};
template <class T>
SingleLinkedLists<T>::SingleLinkedLists(SingleLinkedLists const& value) : head(nullptr), tail(nullptr) {
for(Node* loop = value->head; loop != nullptr; loop = loop->next) {
createNode(loop->data);
}
}
template <class T>
SingleLinkedLists<T>::~SingleLinkedLists() {
while(head != nullptr)
deleteHead();
}
template <class T>
SingleLinkedLists<T>& SingleLinkedLists<T>::operator=(SingleLinkedLists const& rhs) {
SingleLinkedLists copy(rhs);
swap(copy);
}
template <class T>
std::ostream& operator<<(std::ostream& str, SingleLinkedLists<T>& data) {
data.display(str);
return str;
}
template <class T>
void SingleLinkedLists<T>::swap(SingleLinkedLists& other) noexcept {
using std::swap;
swap(head, other.head);
swap(tail, other.tail);
}
template <class T>
void SingleLinkedLists<T>::createNode(const T& theData) {
Node* temp = new Node;
temp->data = theData;
temp->next = nullptr;
if(head != nullptr) {
temp = head;
temp = tail;
temp = nullptr;
}
else {
temp->next = tail;
temp = tail;
}
}
template <class T>
void SingleLinkedLists<T>::createNode(T&& theData) {
Node* temp = new Node;
temp->data = std::move(theData);
temp->next = nullptr;
if(head != nullptr) {
temp = head;
temp = tail;
temp = nullptr;
}
else {
temp->next = tail;
temp = tail;
}
}
template <class T>
void SingleLinkedLists<T>::display() const {
for(Node* loop = head; head != nullptr; loop = loop->next) {
std::cout << loop->data << "\t";
}
}
template <class T>
void SingleLinkedLists<T>::insertHead(const T &theData) {
Node* temp = new Node;
temp->data = theData;
temp->next = head;
head = temp;
}
template <class T>
void SingleLinkedLists<T>::deleteHead() {
Node* old = head;
head = head->next;
delete old;
}
#endif /* SingleLinkedLists_h */
这是main.cpp文件:
#include <iostream>
#include "SingleLinkedLists.h"
int main(int argc, const char * argv[]) {
SingleLinkedLists<int> obj;
obj.createNode(2);
obj.createNode(4);
obj.createNode(6);
obj.createNode(8);
obj.createNode(10);
std::cout<<"\n--------------------------------------------------\n";
std::cout<<"---------------Displaying All nodes---------------";
std::cout<<"\n--------------------------------------------------\n";
std::cout << obj << std::endl;
// std::cout<<"\n--------------------------------------------------\n";
// std::cout<<"-----------------Inserting At End-----------------";
// std::cout<<"\n--------------------------------------------------\n";
// obj.createNode(55);
// std::cout << obj << std::endl;
//
// std::cout<<"\n--------------------------------------------------\n";
// std::cout<<"----------------Inserting At Start----------------";
// std::cout<<"\n--------------------------------------------------\n";
// obj.insert_start(50);
// std::cout << obj << std::endl;
//
// std::cout<<"\n--------------------------------------------------\n";
// std::cout<<"-------------Inserting At Particular--------------";
// std::cout<<"\n--------------------------------------------------\n";
// obj.insert_position(5,60);
// std::cout << obj << std::endl;
//
// std::cout<<"\n--------------------------------------------------\n";
// std::cout<<"----------------Deleting At Start-----------------";
// std::cout<<"\n--------------------------------------------------\n";
// obj.delete_first();
// std::cout << obj << std::endl;
//
// std::cout<<"\n--------------------------------------------------\n";
// std::cout<<"----------------Deleting At End-----------------";
// std::cout<<"\n--------------------------------------------------\n";
// obj.delete_last();
// std::cout << obj << std::endl;
//
//
// std::cout<<"\n--------------------------------------------------\n";
// std::cout<<"--------------Deleting At Particular--------------";
// std::cout<<"\n--------------------------------------------------\n";
// obj.delete_position(4);
// std::cout << obj << std::endl;
// std::cout << std::endl;
//
// obj.search(8) ? std::cout << "Yes" << std::endl : std::cout << "No" << std::endl;
return 0;
}
答案 0 :(得分:1)
您有错误的朋友流运营商声明。它应该看起来像这样:
...
//forward declare template for operator forward declaration
template<class T>
class SingleLinkedLists;
template <class T>
std::ostream &operator<<(std::ostream &, SingleLinkedList<T> &);
template<class T>
class SingleLinkedLists{
...
//specific specialization of template is the friend
friend std::ostream &operator<<(std::ostream &, SingleLinkedList<T> &);
...
};
答案 1 :(得分:1)
您需要添加运算符重载的前向声明:
template<class T> class SingleLinkedLists;
template<class T> std::ostream& operator << (std::ostream& o, const SingleLinkedLists <T>& x);
然后在您的班级定义中,在<>
之后添加operator <<
以涵盖所有模板:
//...
friend std::ostream& operator<< <>(std::ostream& str, const SingleLinkedLists<T>& data);
//...
您的方法声明将如下所示:
template <class T>
std::ostream& operator<< (std::ostream& str, const SingleLinkedLists<T>& data) {
// Some additional stuff
return str;
}