一个单独的List.h头文件:
namespace MobelList{
//
#if !defined(AFX_LIST_H__A00FBDB1_BAC0_476C_83DE_12D836FB14BD__INCLUDED_)
#define AFX_LIST_H__A00FBDB1_BAC0_476C_83DE_12D836FB14BD__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//定义结构
template <typename T>
struct Node
{
Node(T& d):c(d),next(0),pref(0){}
T c;
Node *next,*pref;
};//------------------------------------
//类摸板定义
template <typename T>
class List
{
Node<T> *first,*last;
public:
List(); //构造
void add(T& c); //
void remove(T& c); //
Node<T>* find(T& c); //
void print(); //
~List(); //析构
};//-------------------------------------
#endif // !defined(AFX_LIST_H__A00FBDB1_BAC0_476C_83DE_12D836FB14BD__INCLUDED_)
//
template <typename T>
List<T>::List():first(0),last(0){}
//---------------------------------------
/**
*方法名:add
*功 能:添加
*/
template <typename T>
void List<T>::add(T& n)
{
Node<T>* p=new Node<T> (n);
p->next=first;
first=p;
(last ? p->next->pref:last)=p;
}//--------------------------------------
/**
*方法名:find
*功 能:查找
*/
template <typename T>
Node<T>* List<T>::find(T& n)
{
for(Node<T>* p=first;p;p=p->next)
if(p->c==n) return p;
return 0;
}//-------------------------------------
/**
*方法名:remove
*功 能:移除
*/
template <typename T>
void List<T>::remove(T& n)
{
if(!(Node<T>* p=find(n))) return;
(p->next ? p->next->pref:last) = p->pref;
(p->pref ? p->pref->next:first) = p->next;
delete p;
}//------------------------------------
/**
*方法名:print
*功 能:打印
*/
template <typename T>
void List<T>::print()
{
for(Node<T>* p=first;p;p=p->next)
cout<<p->c<<" ";
cout<<endl;
}
/**
*方法名:~List
*功 能:销毁
*/
template <typename T>
List<T>::~List()
{
for(Node<T>* p;p=first;delete p)
first=first->next;
}
}//end MobelList
//**************************************************************
//main引用:
#include <iostream>
#include <string>
#include "List.h"
using namespace std;
using namespace MobelList;
int main(int argv,char** argc)
{
List<char> *StrList=new List<char>();
StrList->add("mo");
StrList->add("xiaoming");
StrList->print();
delete StrList;
return 0;
}
//**********************************************************
错误提示:
--------------------Configuration: main - Win32 Debug--------------------
Compiling...
main.cpp
D:\vc++\NodeList\main.cpp(9) : error C2664: 'add' : cannot convert parameter 1 from 'char [3]' to 'char &'
A reference that is not to 'const' cannot be bound to a non-lvalue
D:\vc++\NodeList\main.cpp(10) : error C2664: 'add' : cannot convert parameter 1 from 'char [9]' to 'char &'
A reference that is not to 'const' cannot be bound to a non-lvalue
Error executing cl.exe.
main.exe - 2 error(s), 0 warning(s)
即使我用int类型来调摸板也是出错,。不知道为什么,小弟刚接触C++,兄弟们帮看哈。