В C++ я пытаюсь создать Связанный список, который добавит целый класс. Однако, я всегда получаю ту же ошибку:Ошибка: «Неопределенная ссылка на оператора << (std :: ostream &, Dogs const &)»
Undefined reference to operator<<std::ostream&, Dogs const&)
У меня есть 3 файлов, 2 верхних и 1 CPP.
Это как заголовки выглядеть так:
LinkedList.h:
// A class template for holding a linked list.
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <iostream> // For cout and NULL
using namespace std;
template <class T>
class LinkedList
{
private:
// Declare a structure for the list
struct ListNode
{
T value; // The value in this node
struct ListNode *next; // To point to the next node
};
ListNode *head; // List head pointer
public:
// Constructor
LinkedList()
{ head = NULL; }
// Destructor
~LinkedList() {};
// Linked list operations
void appendNode(T);
void displayList() const;
};
//**************************************************
// appendNode appends a node containing the value *
// pased into newValue, to the end of the list. *
//**************************************************
template <class T>
void LinkedList<T>::appendNode(T newValue)
{
ListNode *newNode; // To point to a new node
ListNode *nodePtr; // To move through the list
// Allocate a new node and store newValue there.
newNode = new ListNode;
newNode->value = newValue;
newNode->next = NULL;
// If there are no nodes in the list
// make newNode the first node.
if (!head)
head = newNode;
else // Otherwise, insert newNode at end.
{
// Initialize nodePtr to head of list.
nodePtr = head;
// Find the last node in the list.
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node.
nodePtr->next = newNode;
}
}
//**************************************************
// displayList shows the value *
// stored in each node of the linked list *
// pointed to by head. *
//**************************************************
template <class T>
void LinkedList<T>::displayList() const
{
ListNode *nodePtr; // To move through the list
// Position nodePtr at the head of the list.
nodePtr = head;
// While nodePtr points to a node, traverse
// the list.
while (nodePtr)
{
// Display the value in this node.
cout << nodePtr->value << endl;
// Move to the next node.
nodePtr = nodePtr->next;
}
}
#endif
Dogs.h:
#ifndef Dogs_H
#define Dogs_H
#include <string>
#include <iostream>
using namespace std;
class Dogs;
template <class T>
ostream& operator << (ostream&, const Dogs&);
istream& operator >> (istream&, Dogs&);
class Dogs
{
private:
string dgName;
string dgBreed;
string dgColour;
string dgDad;
string dgMom;
public:
Dogs(string name ="", string breed ="", string colour ="", string dad ="", string mom ="")
{
dgName = name;
dgBreed = breed;
dgColour = colour;
dgDad = dad;
dgMom = mom;
}
friend ostream& operator << (ostream&, const Dogs&);
friend istream& operator >> (istream&, Dogs&);
};
#endif
Я был бы очень признателен, если вы могли бы указать мне, где моя проблема. Спасибо заранее.
В вашем заголовке указано, что 'ostream & operator << (sstream &, const Dog &)' существует, но вы не указали код, определяющий этот оператор. Как вы думаете, почему он существует? –
Как я понял, я не определил оператора. У меня все еще есть смутные знания об операторах. Не могли бы вы выслать мне ссылку на операторов? Было бы признательно. Большое спасибо. – Imad
Оператор - просто фанковое имя для функции. Напишите функцию, которая вставляет объект 'Dog' в объект типа' std :: ostream'. –