简体   繁体   English

错误:“对运算符<<(std :: ostream&,Dogs const&)的未定义引用”

[英]Error: “Undefined reference to operator<<(std::ostream&, Dogs const&)”

On C++, I am trying to make a Linked List which would append a whole class. 在C ++上,我试图创建一个可以附加整个类的链表。 However, I am always getting the same error: 但是,我总是得到同样的错误:

Undefined reference to operator<<std::ostream&, Dogs const&)

I have 3 files, 2 headers and 1 CPP. 我有3个文件,2个标题和1个CPP。

This is how the headers look like: 这是标题的样子:

LinkedList.h: 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: 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

I would really appreciate if you could point me out where my problem is. 如果你能指出我的问题所在,我真的很感激。 Thank you in advance. 先感谢您。

Remove the line with the template specification 删除带有模板规范的行

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

and do not forget to define the operator. 并且不要忘记定义运算符。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “对&#39;std::operator&lt;&lt;(std::ostream&amp;, std::LinkedList const&amp;)的未定义引用”C++ - "Undefined reference to 'std::operator<<(std::ostream&, std::LinkedList const&)" C++ 未定义对 `mtm::operator&lt;&lt;(std::ostream&amp;, mtm::DateWrap const&amp;)' 的引用 - undefined reference to `mtm::operator<<(std::ostream&, mtm::DateWrap const&)' 对`std :: ostream&SpyOutput :: operator &lt;&lt;的未定义引用 <double> (双const&)&#39; - undefined reference to `std::ostream& SpyOutput::operator<< <double>(double const&)' 包括“lvtocon.h”,对`operator&lt;&lt;(std::ostream&amp;, char const*)的未定义引用 - include "lvtocon.h", undefined reference to `operator<<(std::ostream&, char const*) std :: ostream&运算符&lt;&lt;(std :: ostream&sstr,const T&val)的模棱两可的重载 - Ambiguous overload of std::ostream& operator<<(std::ostream& sstr, const T& val) std::ostream&amp; operator&lt;&lt;(std::ostream&amp;, const T&amp;) 未被覆盖 - std::ostream& operator<<(std::ostream&, const T&) not being overridden 未定义对 `operator&lt;&lt;(std::ostream&amp;, /* class 与非类型模板参数 */&amp;)' 的引用 - undefined reference to `operator<<(std::ostream&, /* class with non-type template parameters */&)' 有关初始化类型为“ std :: ostream”的临时类的非常量引用“ std :: ostream&”的C ++编译器错误 - C++ compiler error regarding initialization of a non-const reference of type 'std::ostream&' from a temporary of type 'std::ostream" 错误未定义对BP :: Device :: Create(std :: string const&)的引用 - Error undefined reference to `BP::Device::Create(std::string const&)' 错误:对`cv::imread(std::string const&amp;, int)&#39;的未定义引用 - error: undefined reference to `cv::imread(std::string const&, int)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM