Question

Im making a generic linked list. I cant figure out how to make an object of...

Im making a generic linked list.

I cant figure out how to make an object of my class from main.

My 3 files are main.cpp dan.h dan.cpp

The error is: 15 6 [Error] prototype for 'void ll<T>::insert()' does not match any in class 'll<T>'

#include <iostream>

#include <string>

#include "dan.h"

using namespace std;

int main()

{

ll<int> work;

work.insert(55);//THIS IS THE LINE THATS GIVING ME THE ERROR, Without this line it compiles and //runs.

}

#ifndef DAN_H

#define DAN_H

#include <iostream>

#include <string>

using namespace std;

template<class T>

struct node

{

T data;

node *next;

};

template <class T>

class ll

{

public:

ll();

void insert(T item);

void print();

private:

node<T> head;

int len;

node<T> cur;

};

#endif

#include <iostream>

#include <string>

#include "dan.h"

using namespace std;

template <class T>

ll<T>::ll()

{

head=NULL;

cur=NULL;

len=0;

}

template <class T>

void ll<T>::insert(T item)

{

node<T> temp;

temp->data=item;

if(head==NULL)

{

head=temp;

head->next=NULL;

}

else

{

temp->next=head;

head=temp;

}

}

template <class T>

void ll<T>::print()

{

node<T> temp=head;

while(temp!=NULL)

{

cout<<temp->data<<endl;

temp=temp->next;

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/* NOTE:

Templates need to be declared and implemented in the file you include. You cannot separate template class declaration and implementation and then only include the header file.

With templates, the class is not compiled until it's used. So there is no such thing as a compiled template class that can be linked against. Each time you use a template, it has to be compiled for a different type. And since the compiler does not have access to the implementation, it does not know how to compile it... */

/* SO, SOLUTION IS TO INCLUDE BOTH header and implementation file in main.cpp */

/* main.cpp */

#include <iostream>

#include <string>

#include "dan.h"
#include "dan.cpp"

using namespace std;

int main()

{

ll<int> work;
work.insert(55);
work.print();
}

#include <iostream> #include <string> #include dan.h #include dan.cpp using namespace std; int main() 11<int> work; work.

/*dan.h */

#ifndef DAN_H

#define DAN_H

#include <iostream>

#include <string>

using namespace std;

template<class T>

struct node

{

T data;

node<T> *next;

};

template <class T>

class ll

{

public:

ll();

void insert(T item);

void print();

private:

node<T> *head;

int len;

node<T> *cur;

};


#endif

#ifndef DAN_H #define DAN_H #include <iostream> #include <string> using namespace std; template<class T> struct node I data;

/* dan.cpp */

#include"dan.h"
template <class T>
ll<T>::ll()
{
head=NULL;
cur=NULL;
len=0;

}

template <class T>

void ll<T>::insert(T item)

{

node<T> *temp = new node<T>();

temp->data=item;

if(head==NULL)

{

head=temp;

head->next=NULL;

}

else

{

temp->next=head;

head=temp;

}

}

template <class T>

void ll<T>::print()

{

node<T> *temp=head;

while(temp!=NULL)

{

cout<<temp->data<<endl;

temp=temp->next;

}

}

#include dan.h template <class T> 11<T>::11) head=NULL; cur=NULL; len=0; template <class T> void 1l<T>::insert(T item) node

template <class T> void ll<T>:: print) node<T> *temp=head; while(temp!=NULL) cout<<temp->data<<endl; temp=temp->next;

/* OUTPUT */

55 Process exited after 3.538 seconds with return value o Press any key to continue ...

Add a comment
Know the answer?
Add Answer to:
Im making a generic linked list. I cant figure out how to make an object of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node {     T data;//data field     Node * next;//link field     Node(T data) {       this->data = data;     } }; template<class T> class linked_list{ private:       Node<T> *head,*current; public:       linked_list(){//constructor, empty linked list         head = NULL;         current = NULL;       }       ~linked_list(){         current = head;         while(current != NULL) {          ...

  • there show an error in sample.cpp file that more than one instance of overloaded function find...

    there show an error in sample.cpp file that more than one instance of overloaded function find matches the argument list. can you please fix it. and rewrite the program and debug. thanks. I also wrote error below on which line in sample.cpp. it shows on find. #include #include #include "node1.cpp" using namespace main_savitch_5; // node1.h #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include <string> namespace main_savitch_5 {    template<class item>    class node    {    public:        typedef item value_type;   ...

  • Please fill in the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h>...

    Please fill in the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list...

  • In C++, change this code to a circular linked list #include <iostream> using namespace std; struct...

    In C++, change this code to a circular linked list #include <iostream> using namespace std; struct node { string data; node *next; }; class linked_list { private: node *head,*tail; public: linked_list() { head = NULL; tail = NULL; } void add_node(string n) { node *tmp = new node; tmp->data = n; tmp->next = NULL; if(head == NULL) { head = tmp; tail = tmp; } else { tail->next = tmp; tail = tail->next; } } void display() { while (tmp...

  • can someone please fix this error in the stack file at line 18 ( class Stack2:public...

    can someone please fix this error in the stack file at line 18 ( class Stack2:public Stack { ) this is thew error ----> expected class name before '{' token Queue.h --> #include "Stack.hpp" template <typename ITEM> class Queue { private: ITEM item; Stack <ITEM> * s1= new Stack<ITEM>; Stack <ITEM> * s2= new Stack<ITEM>; public: Queue(){}; bool enqueue (ITEM item); bool dequeue (ITEM &item); bool check = true; ~Queue(); }; //this is the class which contains the functions of...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not ma...

    Problem 1 Given a linked list of integers, write a function getUnique that removes all duplicates elements in the linked list and returns the new linked list of unique elements (The order does not matter). Example: Input: 1-»2->3->1-2 Output: 1->2->3 public class Node f int iterm Node next; Node(int d) t item = d; next-null; ) import java.util.ArrayList; public class ExtraLab public static void main (String[] args)t PROBLEM 1 System.out.println("PROBLEM 1"); Node head new Node(1); head.next-new Node (2); head.next.next-new Node(3);...

  • Double linked list implementation of PutItem function. How to fix my code to get desired output b...

    Double linked list implementation of PutItem function. How to fix my code to get desired output below: Output: 2 5 8 #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void setValue(int newValue);     int getValue() const;     RelationType ComparedTo(ItemType newItem); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::setValue(int newValue) {     value = newValue; } int ItemType::getValue() const {     return value; } RelationType ItemType::ComparedTo(ItemType newItem)...

  • Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include...

    Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...

  • In C++, for the provided template linked list class create a derived class of it which...

    In C++, for the provided template linked list class create a derived class of it which adds the functionality to it to find the high and low value of any given data stored in the list. The derived class must be a template. LinkedList.h #pragma once #include <iostream> using namespace std; template <class T> class ListNode {    public:        T data;        ListNode<T>* next;        ListNode(T data)        {            this->data = data;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT