找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 1977|回复: 6

[求助]摸板类出问题了。。。。。。。

[复制链接]
发表于 2009-11-4 00:29:33 | 显示全部楼层 |阅读模式 IP:江苏扬州
一个单独的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++,兄弟们帮看哈。
发表于 2009-11-4 00:29:34 | 显示全部楼层 IP:江苏扬州
Node<T>* List<T>::find(T& n)

这里如果要是变成char 就是这样
Node* List::find(char& n)
你看你传(“MO”)还对么/
改成指针
回复

使用道具 举报

发表于 2009-11-4 00:29:35 | 显示全部楼层 IP:江苏扬州
前后不一致啊。

typedef string T;

List<T> *StrList=new List<T>();
StrList->add(T("mo"));
StrList->add(T("xiaoming"));
回复

使用道具 举报

发表于 2009-11-4 00:29:36 | 显示全部楼层 IP:江苏扬州
还是出问题,我知道了,我改去掉引用T&amp;的&amp;就可以了,请问有什么分别?
回复

使用道具 举报

发表于 2009-11-4 00:29:37 | 显示全部楼层 IP:江苏扬州
以下是引用ming206在2006-10-11 12:21:25的发言:
还是出问题,我知道了,我改去掉引用T&的&就可以了,请问有什么分别?
这也不对吧,用 T*
回复

使用道具 举报

发表于 2009-11-4 00:29:38 | 显示全部楼层 IP:江苏扬州
你用的形参是一个字符变量,而你在调用时用的是
 字符串,在隐式将字CHAR 转为字符串的做法是
在形参中用 T* 来代替模板中的变量
回复

使用道具 举报

发表于 2009-11-4 00:29:39 | 显示全部楼层 IP:江苏扬州
同三楼wfpb
List<string> *StrList=new List<string>();
StrList->add(string("mo"));
StrList->add(string("xiaoming"));
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-30 21:22 , Processed in 0.209792 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表