刚才没有发好,重发,关于钱能C++第二版的一道例题,怪了....大家有时间的话来帮帮忙(加分!!!)
小弟在看到钱能第二版C++的模板这一章时,看了书上的讲解,觉得讲的很生动,让我很容易就理解模板的概念和使用,书上也提供了完整的例子作为讲解,但是当我把书上的例子亲自动手写在电脑上运行时,却出现了意想不到结果...一堆错误消息,是编译器的问题还是概念的问题..我糊涂了...望大家有时间来试着编译一下这个例子,若遇到和我一样的问题,请帮我想想怎么解决,谢谢拉,我能想的都想了....,编译器我用的是Devcpp,后来又用VC++6.0,问题依然如故...程序例如下:
#include <iostream>
using namespace std;
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();
};
template <typename T>
List <T> ::List():first(0),last(0){}
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 ;
}
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;
}
template <typename T>
Node <T> * List <T> ::find(T& n)
{
for(Node <T> * p=first;p;p-> next)
if(p-> c==n) return p;
return 0;
}
template <typename T>
List <T> ::~List()
{
for(Node <T> * p;p=first;delete p)
first=first-> next;
}
template <typename T>
void List <T> ::print()
{
for(Node <T> * p=first;p;p=p-> next)
cout < <p-> c < < " ";
cout < < "\n ";
}
int main()
{
List <double> dList;
dList.add(3.6);
dList.add(5.8);
dList.print();
List <int> iList;
iList.add(5);
iList.add(8);
iList.print();
}
错误代码为:
35 C:\Documents and Settings\winsd\My Documents\C++练习\test.cpp expected primary-expression before '* ' token
35 C:\Documents and Settings\winsd\My Documents\C++练习\test.cpp `p ' undeclared (first use this function)
63 C:\Documents and Settings\winsd\My Documents\C++练习\test.cpp no matching function for call to `List <double> ::add(double) '
64 C:\Documents and Settings\winsd\My Documents\C++练习\test.cpp no matching function for call to `List <double> ::add(double) '
67 C:\Documents and Settings\winsd\My Documents\C++练习\test.cpp no matching function for call to `List <int> ::add(int) '
68 C:\Documents and Settings\winsd\My Documents\C++练习\test.cpp no matching function for call to `List <int> ::add(int) '
大家帮帮我啊~先谢谢拉!
[解决办法]
//一是将模板定义处参数加上const(下面的示例是这种,VC6通过)
//二是定义变量赋值后传入
#include <iostream>
using namespace std;
template <typename T>
struct Node
{
Node(const 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(const T& c);
void remove(T& c);
Node <T> * find(T& c);
void print();
~List();
};
template <typename T>
List <T> ::List():first(0),last(0){}
template <typename T>
void List <T> ::add(const T& n)
{
Node <T> * p=new Node <T> (n);
p-> next=first;
first=p;
(last?p-> next-> pref:last)=p ;
}
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;
}
template <typename T>
Node <T> * List <T> ::find(T& n)
{
for(Node <T> * p=first;p;p-> next)
if(p-> c==n) return p;
return 0;
}
template <typename T>
List <T> ::~List()
{
for(Node <T> * p;p=first;delete p)
first=first-> next;
}
template <typename T>
void List <T> ::print()
{
for(Node <T> * p=first;p;p=p-> next)
cout < <p-> c < < " ";
cout < < "\n ";
}
int main()
{
List <double> dList;
dList.add(3.6); //因为是引用,不能这样给个常量
//一是将模板定义处参数加上const
//二是定义变量赋值后传入
dList.add(5.8);
dList.print();
List <int> iList;
iList.add(5);
iList.add(8);
iList.print();
return 0;
}
[解决办法]
if(!(Node <T> * p=find(n)))return;
这行是有问题的吧,改成
Node <T> * p;
if (!(p = find(n))) return;
就可以了。
不过后面还有一些问题,List::add的定义中要求引用传参,而后面调用时的实参是常量,常量是不可以传引用的。