这个C++代码有问题。我用VC++ 6.0编译它。它给出错误“无法推断出类型的模板参数”...问题出在display
函数中。在结构上实现的C++模板
下面的代码:
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
template <class type>
struct one
{
type data;
one *next;
};
one<int> *head, *temp, *mid, *del = NULL;
template <class type>
void athead(type value)
{
one *node = new one;
node->data = value;
node->next = head;
head = node;
}
template <class type>
void add_at_tail(type value)
{
one *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 the data: " << endl;
cin >> a;
add_at_tail(a);
cout << "Enter the data: " << endl;
cin >> b;
add_at_tail(b);
cout << "Enter the data: " << endl;
cin >> c;
add_at_tail(c);
display();
return 0;
}
这看起来像家庭作业。是吗? – 2011-04-29 14:21:32