linker error

This is a discussion on linker error within the C++ Programming forums, part of the General Programming Boards category; I am trying to fix a linker error the error basically states this when I type make: g++ -Wall -ansi ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    25

    linker error

    I am trying to fix a linker error the error basically states this when I type make:

    g++ -Wall -ansi -c Proj5Aux.cpp
    g++ -Wall -ansi -o Proj5 Proj5.cpp /afs/umbc.edu/users/d/a/dana3/pub/CMSC202/p4/KLSocket.o Proj5Aux.o Human.o Computer.o Player.o Boards.o Node.o Square.o Queue.o Exception.o
    Proj5Aux.o: In function `Queue<Square>::Enqueue(Square const&)':
    Proj5Aux.cpp.gnu.linkonce.t._ZN5QueueI6SquareE7EnqueueERKS0_+0 x24): undefined reference to `Node<Square>::Node(Square const&)'
    collect2: ld returned 1 exit status
    make: *** [Proj5] Error 1

    here is my makefile

    Code:
    COMPILER=g++
    CFLAGS= -Wall -ansi
    PROJECT1=Proj5
    PROJECT2=Tourney
    SOCKET=/afs/umbc.edu/users/d/a/dana3/pub/CMSC202/p4/KLSocket.o
    
    $(PROJECT1) : Proj5.cpp $(SOCKET) Proj5Aux.o Boards.o Square.o Node.o Queue.o P\layer.o Human.o Computer.o Exception.o
            $(COMPILER) $(CFLAGS) -o $(PROJECT1) Proj5.cpp $(SOCKET) Proj5Aux.o Hum\an.o Computer.o Player.o Boards.o Node.o Square.o Queue.o Exception.o
    
    Proj5Aux.o : Proj5Aux.cpp Proj5Aux.h
            $(COMPILER) $(CFLAGS) -c Proj5Aux.cpp
    
    Human.o : Human.cpp Human.h
            $(COMPILER) $(CFLAGS) -c Human.cpp
    
    Computer.o : Computer.cpp Computer.h
            $(COMPILER) $(CFLAGS) -c Computer.cpp
    
    Player.o : Player.cpp Player.h
     $(COMPILER) $(CFLAGS) -c Player.cpp
    
    Boards.o : Boards.cpp Boards.h
            $(COMPILER) $(CFLAGS) -c Boards.cpp
    
    Node.o: Node.cpp Node.h
            $(COMPILER) $(CFLAGS) -c Node.cpp
    
    Square.o: Square.cpp Square.h
            $(COMPILER) $(CFLAGS) -c Square.cpp
    
    
    Queue.o: Queue.cpp Queue.h
            $(COMPILER) $(CFLAGS) -c Queue.cpp
    
    Tourney.o: Tourney.cpp Tourney.h
            $(COMPILER) $(CFLAGS) -c Tourney.cpp
    
    PlayerInfo.o: PlayerInfo.cpp PlayerInfo.h
            $(COMPILER) $(CFLAGS) -c PlayerInfo.cpp
    
    Exception.o: Exception.cpp Exception.h
             $(COMPILER) $(CFLAGS) -c Exception.cpp
    If u need to see files as well ask as u can see its about twenty of them

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by tasha302
    If u need to see files as well ask as u can see its about twenty of them
    Maybe just the one that defines Node<Square>::Node(Square const&) for now. Later folks may need to know the name of this header file and how it is #included in the file that contains Queue<Square>::Enqueue(Square const&).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    25
    Code:
    
    #ifndef NODE_H
    #define NODE_H
    
    #include <iostream>
    #include "Square.h"
    
    using namespace std;
    template <class T> class Node;
    template <class T> class Queue;
    
    template< class T >
    
    class Node{
    
     public:
    
      //**********************************************************
      //Function     :Default constructor
      //PreCondition :None
      //PostCondition:data row = col =0, next = NULL
      //**********************************************************
       Node();
    
    
      //**********************************************************
      //Function     :Constructor
      //PreCondition :None
      //PostCondition:row and col set to value passed in, next = NULL
      //**********************************************************
    
       Node(const T&);
    
      //**********************************************************
      //Function     :retrieves next pointer
      //PreCondition :none
      //PostCondition:next pointer is returned
      //**********************************************************
    
      Node< T > *GetNext();
    
      //**********************************************************
      //Function     :sets next pointer
      //PreCondition :none
      //PostCondition:next = nodeptr
      //**********************************************************
    
      void SetNext(Node< T > *nodeptr);
    
      //**********************************************************
      //Function     :retrieves data in node
      //PreCondition :none
      //PostCondition:class containing data is returned
      //**********************************************************
    
      const T& GetData();
    
     private:
    
      T m_data;
      Node< T > *m_next;
      friend class Queue<T>;
    };
    
    
    #include "Node.cpp"
    
    #endif
    //implementation

    Code:
    
    #ifndef NODE_CPP
    #define NODE_CPP
    
    #include "Node.h"
    
    using namespace std;
    
    
    //**********************************************************
    //Function     :Default constructor
    //PreCondition :
    //PostCondition:data holds two int = 0, next = NULL
    //**********************************************************
    
    template< class T >
    Node<T>::Node() : m_data()
    {
      m_next = NULL;
    }
    
    //**********************************************************
    //Function     :constructor
    //PreCondition :None
    //PostCondition:row = row column =row next = NULL
    //**********************************************************
    
    template< class T >
    Node<T>::Node(const T& Node_Data): m_data(Node_Data)
    {
      m_next = NULL;
    }
    
    //**********************************************************
    //Function     :get the next pointer
    //PreCondition :none
    //PostCondition:returns the next pointer
    //**********************************************************
    
    template< class T >
    Node< T>* Node<T>::GetNext()
    {
      return m_next;
    }
    
    //**********************************************************
    //Function     : sets next pointer
    //PreCondition : none
    //PostCondition:next = nodeptr
    //**********************************************************
    
    template< class T >
    void Node<T>::SetNext(Node<T> *nodeptr)
    {
      m_next = nodeptr;
    }
    
    //**********************************************************
    //Function     :retrieves data
    //PreCondition :none
    //PostCondition:data of the square class is returned
    //**********************************************************
    
    template< class T >
    const T& Node<T>::GetData()
    {
      return m_data;
    }
    
    
    #endif
    //Queue Class

    Code:
    
    #ifndef QUEUE_CPP
    #define QUEUE_CPP
    
    
    #include "Queue.h"
    #include <assert.h>
    #include "Exception.h"
    
    using namespace std;
    
    //********************************************************
     //Function     : Constructor
     //PreCondition :
     //PostCondition:
     //********************************************************
     
    template< class T >
    Queue<T>::Queue()
    {
      m_head = NULL;
      m_tail = NULL;
    }
    
    //********************************************************
    //Function     : Destructor
    //PreCondition :
    //PostCondition: All things in queue would be destroyed
    //********************************************************
    
    template< class T >
    Queue<T>::~Queue()
    {
      DestroyQueue();
    
      m_head = NULL;
      m_tail = NULL;
    
    }
    
    
    //********************************************************
    //Function     : copy constructor
    //PreCondition : object is not empty
    //PostCondition: object is copied into another object
    //********************************************************
    
    template< class T >
    Queue<T>::Queue(const Queue<T>& A_Queue)
    {
      m_head = m_tail = NULL;
      CopyQueue(A_Queue);
    }
    
    
    
    //********************************************************
    //Function     : assignment operator
    //PreCondition : not attempting a self copy
    //PostCondition: object is copied into another object
    //********************************************************
    
    template< class T >
    Queue<T>& Queue<T>::operator= (const Queue<T>& A_Queue)
    {
      if(this != &A_Queue)
        CopyQueue(A_Queue);
      else
        throw ExceptClass("Attempting a self copy");
      return *this;
    
    }
      
    //********************************************************
    //Function     : check if Queue is empty
    //PreCondition : none
    //PostCondition: true or false is returned 
    //********************************************************
    
    template< class T >
    bool Queue<T>::IsEmpty()const
    {
      return (m_head == NULL);
    }
    
    
    //********************************************************
    //Function     : places item into queue
    //PreCondition : item being put in is not empty row and col
    //               are between 0 and 9
    //PostCondition: item is placed at the front of the queue
    //********************************************************
    
    template< class T >
    void Queue<T>::Enqueue(const T& Queuedata)
    {
     
       
         Node<T> *newNode;
         newNode = new Node<T>(Queuedata);
    
         if(newNode == NULL)
           throw ExceptClass("Attempting to Enqueue empty data");
    
         if(m_head == NULL)
          {
           m_head = newNode;
           m_tail = newNode;
         
          }
         else
          {
           m_tail->m_next = newNode;
           m_tail = m_tail->m_next;
         
          }
       
    
    }
    
    
    
    //********************************************************
    //Function     : deletes item in queue(first item)
    //PreCondition : queue is not empty
    //PostCondition: first item is deleted from queue
    //********************************************************
    
    template< class T >
    void Queue<T>::Dequeue()
    {
      Node<T> *temp;
    
      if(!IsEmpty())
        {
          temp = m_head;
          m_head = m_head->m_next;
          delete temp;
          if(m_head == NULL)
    	m_tail = NULL;
        }
      else
        throw ExceptClass("Queue is empty:: Nothing to remove");
    }
    
    
    //********************************************************
    //Function     : retrieves item in the front of the queue
    //PreCondition : queue is not empty
    //PostCondition: item is returned to caller
    //*******************************************************
    
    template< class T >
    const T& Queue<T>::Front()
    {
      if( m_head == NULL)
        throw ExceptClass("Queue is empty: Nothing to retrieve");
    
       return m_head->m_data;
    }
    
    
    
    //********************************************************
    //Function     : retrieves item from the back of the queue
    //PreCondition : queue is not empty
    //PostCondition: item is returned to caller
    //********************************************************
    
    template< class T >
    const T& Queue<T>::Back()
    {
      if (m_head == NULL)
        throw ExceptClass("Queue is empty: Nothing to retrieve");
    
      return m_tail->m_data;
    }
    
    
    //********************************************************
    //Function     : Destroys every item in a queue
    //PreCondition : queue is not empty
    //PostCondition: queue is destroyed
    //********************************************************
    
    template< class T > 
    void Queue<T>::DestroyQueue()
    {
      Node<T> *temp;
    
      while(!IsEmpty())
        {
          temp = m_head;
          m_head = m_head->m_next;
          delete temp;
        }
    }
    
    
    
    //********************************************************
    //Function     : copies item from one queue to another queue
    //PreCondition : queue is not empty
    //PostCondition: object is copied to another object
    //********************************************************
    
    template< class T >
    void Queue<T>::CopyQueue(const Queue<T>& A_Queue)
    {
      if(m_head != NULL)
       m_head = m_tail = NULL;
      if(A_Queue.m_head == NULL)
        m_head = m_tail = NULL;
      else
        {
          Node<T> *temp;
    
          temp = A_Queue.m_head;
          m_head = new Node<T>;
    
          m_head->m_data = temp->m_data;
          m_head->m_next = NULL;
         
          m_tail = m_head;
    
          temp = temp->m_next;
    
          while(temp!= NULL)
    	{
    	  Node<T> *newNode = new Node<T>;
    
    	  newNode->m_data = temp->m_data;
    	  newNode->m_next = NULL;
    	  m_tail->m_next = newNode;
    	  m_tail = newNode;
    	  temp = temp->m_next;
    	}
        }
    }
    
    
    
    //*********************************************************************
    //Function     : Outputs item in the queue to the screen
    //Precondition : queue is not empty
    //PostCondition: item is sent to the screen
    //*********************************************************************
    template<class T>
    ostream& operator<< (ostream& out, const Queue<T>& A_Queue)
    {
    
      Node<T> *temp = new Node<T>;
    
      temp = A_Queue.m_head;
    
      if(A_Queue.IsEmpty())
        throw ExceptClass("Queue is Empty: Nothing to output.");
      else
        {
          while(temp != NULL)
    	{
    	  out<<"Items in queue are: "<<temp->GetData();
    	  temp = temp->GetNext();
    	}
        }
      delete temp;
      return out;
    }
    
    
    
    #endif

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    You can't separate templated classes into header and source files like you do with normal classes. One solution is to place all of your cpp file code into the corresponding header file for the classes that are templated.

    For more information see this FAQ and a few below it:

    http://www.parashift.com/c++-faq-lit...html#faq-35.12

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21