there wrong c++ code. i've compiled in vc ++ 6.0. it's giving error "cannot deduce template argument type"... problem in display
function.
here's code:
#include "stdafx.h" #include <iostream> #include <conio.h> using namespace std; template <class type> struct 1 { type data; 1 *next; }; one<int> *head, *temp, *mid, *del = null; template <class type> void athead(type value) { 1 *node = new one; node->data = value; node->next = head; head = node; } template <class type> void add_at_tail(type value) { 1 *node = new one; node->data = value; node->next = null; if(head == null) { head = node; temp = head; } while(temp->next != null) { temp = temp->next; } if(temp != node) { temp->next = node; } } template <class type> void display() { one<type> *temp = new one; temp = head; cout << "\n\n" << endl; while(temp != null) { cout << " " << temp->data << " " << "->"; temp = temp->next; } cout << "\n\n\n"; } int main() { int a, b, c; cout << "enter data: " << endl; cin >> a; add_at_tail(a); cout << "enter data: " << endl; cin >> b; add_at_tail(b); cout << "enter data: " << endl; cin >> c; add_at_tail(c); display(); return 0; }
couple problems here, @ least:
first, have defined template function:
template<class type> void display() { one<type> *temp=new one; temp=head; cout<<"\n\n"<<endl; while(temp!=null) { cout<<" "<<temp->data<<" "<<"->"; temp=temp->next; } cout<<"\n\n\n"; }
this line malformed, because one
not complete type (one
class template)
one<type> *temp=new one;
you need this:
one<type> *temp=new one<type>;
then down in client code, attempt call function template this:
display();
but display
function template no arguments, there's no way compiler can deduce type of it's template parameter type
. must specify type of type
@ call point.
display<int>();
there logic errors implementation of display()
. instantiate single copy of one
, don't initialize it. try iterate on it's linked list, it's not -- uninitialized node created. want pass in linked list you're trying iterate over. along these lines:
template<class type> void display(const one<type>& head) { one<type> const * temp = &head; cout<<"\n\n"<<endl; while(temp!=null) { cout<<" "<<temp->data<<" "<<"->"; temp=temp->next; } cout<<"\n\n\n"; }
now display
takes parameters mentioned in template, compiler able deduce type. want call this:
display(head);
Comments
Post a Comment