C++ 编译报错...不知道错在哪里...

2024-11-23 19:27:33
推荐回答(4个)
回答1:

#include
#include
using namespace std;

template
struct Node
{
Node(const T& d):c(d),next(0),pref(0){} // 这里加了const
T c;
Node *next, *pref;
};

template
class List
{
Node *first, *last;
public:
List();
void add(const T& c); // 这里加了const
void remove(const T& c); // 这里加了const
Node* find(const T& c); // 这里加了const
void print();
~List();
};

template
List::List():first(0),last(0){}

template
void List::add(const T& n) // 这里加了const
{
Node* p = new Node(n);
p->next = first; first = p;
(last ? p->next->pref : last) = p;
}

template
void List::remove(const T& n) // 这里加了const
{
if(!(Node *p = find(n))) return 0;
(p->next ? p->next->pref : last) = p->pref;
(p->pref ? p->pref->next : first) = p->next;
delete p;
}

template
Node* List::find(const T& n) // 这里加了const
{
for(Node* p=first; p; p=p->next)
if(p->c==n) return p;
return 0;
}

template
List::~List()
{
for(Node* p; p=first; delete p)
first = first->next;
}

template
void List::print()
{
for(Node* p=first; p; p=p->next)
cout<c<<" ";
cout<<"\n";
}

int main()
{
List dList;
dList.add(3.6); //因为这里传的是常量,所以形参要是const,以防止在函数中对常量作修改,同理其他函数
dList.add(5.8);
dList.print();
List iList;
iList.add(5);
iList.add(8);
iList.print();
getch();
return 0;
}

回答2:

#include
#include
using namespace std;

template
struct Node
{
Node(T& d):c(d),next(0),pref(0){}
T c;
Node *next, *pref;
};

template
class List
{
Node *first, *last;
public:
List();
void add(T& c);
void remove(T& c);
Node* find(T& c);
void print();
~List();
};

template
List::List():first(0),last(0){}

template
void List::add(T& n)
{
Node* p = new Node(n);
p->next = first; first = p;
(last ? p->next->pref : last) = p;
}

template
void List::remove(T& n)
{
if(!(Node *p = find(n))) return 0;
(p->next ? p->next->pref : last) = p->pref;
(p->pref ? p->pref->next : first) = p->next;
delete p;
}

template
Node* List::find(T& n)
{
for(Node* p=first; p; p=p->next)
if(p->c==n) return p;
return 0;
}

template
List::~List()
{
for(Node* p; p=first; delete p)
first = first->next;
}

template
void List::print()
{
for(Node* p=first; p; p=p->next)
cout<c<<" ";
cout<<"\n";
}

int main()
{
List dList;
//dList.add(3.6); //错误C2664: “List::add”: 不能将参数 1 从“double”转换为“double &”
//dList.add(5.8); //错误C2664: “List::add”: 不能将参数 1 从“double”转换为“double &”
double a=3.6,b=5.8;
dList.add(a);
dList.add(b);
dList.print();

List iList;
//iList.add(5); //错误 C2664: “List::add”: 不能将参数 1 从“int”转换为“int &”
//iList.add(8); //错误 C2664: “List::add”: 不能将参数 1 从“int”转换为“int &”
int c=5,d=8;
iList.add(c);
iList.add(d);
iList.print();
getch();
return 0;/////////////////////应该有返回值
}

回答3:

引用是不能传常量的。
int main()
{
List dList;
double a = 3.6, b = 5.8;
int c = 5, d = 8;
dList.add(a);
dList.add(b);
dList.print();
List iList;
iList.add(c);
iList.add(d);
iList.print();
getch();
}

回答4:

#include"stdafx.h"
#include
#include
using namespace std;

template
struct Node
{
Node(T& d):c(d),next(0),pref(0){}
T c;
Node *next, *pref;
};

template
class List
{
Node *first, *last;
public:
List();
void add(T c);
void remove(T& c);
Node* find(T& c);
void print();
~List();
};

template
List::List():first(0),last(0){}

template
void List::add(T n)
{
Node* p = new Node(n);
p->next = first; first = p;
(last ? p->next->pref : last) = p;
}

template
void List::remove(T& n)
{
if(!(Node *p = find(n))) return 0;
(p->next ? p->next->pref : last) = p->pref;
(p->pref ? p->pref->next : first) = p->next;
delete p;
}

template
Node* List::find(T& n)
{
for(Node* p=first; p; p=p->next)
if(p->c==n) return p;
return 0;
}

template
List::~List()
{
for(Node* p; p=first; delete p)
first = first->next;
}

template
void List::print()
{
for(Node* p=first; p; p=p->next)
cout<c<<" ";
cout<<"\n";
}

int main()
{
List dList;
dList.add(3.6);
dList.add(5.8);
dList.print();
List iList;
iList.add(5);
iList.add(8);
iList.print();
getch();
}