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;   ...

  • 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)...

  • 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;...

  • 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...

  • Q) Modify the class Linked List below to make it a Doubly Linked List. Name your...

    Q) Modify the class Linked List below to make it a Doubly Linked List. Name your class DoublyLinkedList. Add a method addEnd to add an integer at the end of the list and a method displayInReverse to print the list backwards. void addEnd(int x): create this method to add x to the end of the list. void displayInReverse(): create this method to display the list elements from the last item to the first one. Create a main() function to test...

  • I am trying to make a linked list queue and I am trying to use the...

    I am trying to make a linked list queue and I am trying to use the display method I made just to see if its working but when I run it nothing is displayed please help. Also the newPlane boolean was made just so I can randomly decide if the plane is going to join the queue or not. public class PlaneSimulation { public static void main(String[] args) { int landTime = 2; int takeoffTime = 3; int avgArrivalInterval =...

  • Requirements: Finish all the functions which have been declared inside the hpp file. Details: st...

    Requirements: Finish all the functions which have been declared inside the hpp file. Details: string toString(void) const Return a visible list using '->' to show the linked relation which is a string like: 1->2->3->4->5->NULL void insert(int position, const int& data) Add an element at the given position: example0: 1->3->4->5->NULL instert(1, 2); 1->2->3->4->5->NULL example1: NULL insert(0, 1) 1->NULL void list::erase(int position) Erase the element at the given position 1->2->3->4->5->NULL erase(0) 2->3->4->5->NULL //main.cpp #include <iostream> #include <string> #include "SimpleList.hpp" using std::cin; using...

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
ADVERTISEMENT