Results 1 to 4 of 4

Thread: Errors compiling a simple 3 file C++ project

Hybrid View

  1. #1
    Join Date
    Feb 2008
    Location
    Bangalore
    Beans
    55

    Question Errors compiling a simple 3 file C++ project

    linked_list.h
    Code:
    /* linked_list.h */
    #ifndef _LINKED_LIST_H
    #define _LINKED_LIST_H
    
    #include<iostream>
    
    template <class T>
    class LinkedList
    {
      struct node {
        T data;
        struct node* next;
      };
    
    
    
    public:
      LinkedList();
      void add_node(T data, int position);
      void delete_node(T data);
      void print();
    };
    
    #endif
    linked_list.cpp
    Code:
    /* linked_list.cpp */
    #include<iostream>
    #include "linked_list.h"
    using namespace std;
    
    template <class T>
    LinkedList<T>::LinkedList()
    {
      head = NULL;
    }
    
    template <class T>
    void LinkedList<T>::add_node(T data, int position)
    {
      struct node** pp = &head;
      int current_position = 0;
      while(*pp != NULL && position != current_position) {
        ++current_position;
        pp = &(*pp)->next;
      }
    
      struct node* temp = new node();
      temp->data = data;
      temp->next = *pp;
      *pp = temp;
    }
    
    template <class T>
    void LinkedList<T>::print()
    {
      struct node* temp = head;
      int count = 0;
      while(temp != NULL) {
        std::cout<<temp->data<<endl;
        temp = temp->next;
      }
    }
    
    template<class T>
    void LinkedList<T>::delete_node(T data)
    {
      struct node** pp = &head;
      while(*pp != NULL && (*pp)->data != data)
      {
        pp = &(*pp)->next;
      }
      if (pp != NULL)
      {
        node *temp = *pp;
        *pp = (*pp)->next;
        delete(temp);
      }
      else
        cout<<"Data: "<<data<<" not found in the list"<<endl;
    }
    m.cpp
    Code:
    /* m.cpp */
    #include<iostream>
    #include "linked_list.h"
    using namespace std;
    
    int main() {
      LinkedList<int> l;
      l.add_node(1,0);
      l.add_node(2,1);
      l.add_node(3,2);
      l.add_node(4,3);
      l.add_node(6,4);
      l.add_node(5,4);
      l.add_node(7,6);
      l.print();
      return 0;
    }
    The command I run to compile, link and create an executable:
    Code:
    g++ linked_list.cpp m.cpp
    Error:
    Code:
    linked_list.cpp: In constructor ‘LinkedList<T>::LinkedList()’:
    linked_list.cpp:8:3: error: ‘head’ was not declared in this scope
       head = NULL;
       ^
    linked_list.cpp: In member function ‘void LinkedList<T>::add_node(T, int)’:
    linked_list.cpp:14:23: error: ‘head’ was not declared in this scope
       struct node** pp = &head;
                           ^
    linked_list.cpp: In member function ‘void LinkedList<T>::print()’:
    linked_list.cpp:30:23: error: ‘head’ was not declared in this scope
       struct node* temp = head;
                           ^
    linked_list.cpp: In member function ‘void LinkedList<T>::delete_node(T)’:
    linked_list.cpp:41:23: error: ‘head’ was not declared in this scope
       struct node** pp = &head;
                           ^
    Any idea what I am doing wrong here? Actually this code worked fine as long as it was in a single .cpp file. As soon as I split it, I get errors like this or linker errors saying symbol not found.

  2. #2
    Join Date
    Sep 2007
    Beans
    32
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Errors compiling a simple 3 file C++ project

    You need to declare "head" in your class as type
    Code:
    struct node * head;
    .
    Last edited by mvs1207; July 15th, 2013 at 05:10 PM.

  3. #3
    Join Date
    Sep 2007
    Beans
    32
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Errors compiling a simple 3 file C++ project

    For templates in different file, I found this link in stack overflow.

    HTH

  4. #4
    Join Date
    Feb 2008
    Location
    Bangalore
    Beans
    55

    Re: Errors compiling a simple 3 file C++ project

    Thanks for the pointer to the Stackoverflow discussion. It was helpful.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •