2016-12-13 151 views
1

在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 

我真的很感激,如果你能点我出了我的问题在哪里。 预先感谢您。

+0

您的标题说'ostream&operator <<(sstream&,const Dog&)'存在,但是您没有显示任何定义该运算符的代码。你为什么认为它存在? –

+0

据我所知,我没有定义一个操作符。我仍然对运营商有一个模糊的认识。你能给我发一个关于运营商的链接吗?这将不胜感激。非常感谢你。 – Imad

+0

运算符只是一个函数的时髦名称。编写一个将'Dog'对象插入'std :: ostream'类型的对象的函数。 –

回答

0

与模板规范

template <class T> 
^^^^^^^^^^^^^^^^^^ 
ostream& operator << (ostream&, const Dogs&); 

删除线,不要忘记定义操作。

+0

感谢您的快速回复。但是,您能否更精确地定义运营商?谢谢! – Imad

+0

@Imad标题中只有操作员的声明。你必须定义它,你需要编写代码这个操作符将如何输出类Dog的对象。 –

+0

我试图在** Dogs.h **中定义一个运算符,但是它仍然显示相同的错误。这里是我写的'狗操作符+(const狗&a)狗狗; \t \t dog.dgName = this-> dgName + a。dgName; \t \t dog.dgBreed = this-> dgBreed + a.dgBreed; \t \t dog.dgColour = this-> dgColour + a.dgColour; \t \t dog.dgDad = this-> dgDad + a.dgDad; \t \t dog.dgMom = this-> dgMom + a.dgMom; \t \t归还狗; \t}' – Imad

相关问题