timeturn 2017-11-20 13:20 采纳率: 75%
浏览 1320

c++使用模版出现类型不匹配

#include
using namespace std;

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

template
class List{
Nodefirst, *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
Node*List::find(T& n)
{
for(Node*p=first;p;p->next)
if(p->c==n)
return p;
return 0;
}

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

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

int main(){
List dlist;
double a=3.6,b=5.8;
dlist.add(a);
dlist.add(b);
dlist.print();

}

error C2039: 'next' : is not a member of 'Node'
while compiling class-template member function 'void __thiscall List::add(double &)'

  • 写回答

1条回答 默认 最新

  • 橡木疙瘩 2018-03-12 05:21
    关注

    Node中next和pref的定义拿到下面的两个“}"之间去。
    它们被放在构造函数中了,自然就成了局部变量而不是成员变量。

    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料