一个不能理解的错误(继)

tobeistodo1 2004-09-28 05:21:52
http://search.csdn.net/Expert/topic/610/610745.xml?temp=.6483728
我遇到了与此贴同样的问题,但当我按此方法实施后,却出现了另一种错误:
Compiling...
main.cpp
error C2440: 'initializing' : cannot convert from 'class Node<float> *' to 'float *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
while compiling class-template member function '__thiscall dataList<float>::dataList<float>(int)'
hstaticsearch.h(39) : error C2439: 'Element' : member could not be initialized
see declaration of 'Element'
while compiling class-template member function '__thiscall dataList<float>::dataList<float>(int)'
Error executing cl.exe.
请高手指教?
另:对value如何设置缺省值?

...全文
200 13 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
tobeistodo1 2004-09-29
  • 打赏
  • 举报
回复
谢谢大家!
wxfnj 2004-09-28
  • 打赏
  • 举报
回复
template <class Type> class dataList;

//
template <class Type> class Node //Êý¾Ý±íÖнáµãÀàµÄ¶¨Òå
{
friend class dataList<Type>;

public:
Node(const Type &value)
{
// value = 0;
key = value;
}

Node(){}

Type getKey() const //¶ÁÈ¡¹Ø¼üÂë
{
return key;
}

void setKey(Type k) //Ð޸ĹؼüÂë
{
key = k;
}


Type key; //¹Ø¼üÂëÓò
Type other; //ÆäËûÓò
};

//
template <class Type> class dataList //Êý¾Ý±íÀඨÒå
{
public:
dataList(int sz = 10): Element(new Node<Type>[sz]), ArraySize(sz){} //¹¹Ô캯Êý

virtual ~dataList()
{
delete [] Element;
}

//ÖØÔØ"<<" ºÍ ">>"
friend ostream &operator << (ostream &OutStream, const dataList <Type> & OutList);
friend istream &operator >> (istream &InStream, dataList <Type> & InList);

protected:
Node<Type> *Element; //Êý¾Ý±íÖд洢Êý¾ÝµÄÊý×é
int ArraySize; //Êý×é×î´ó³¤¶È
int CurrentSize; //Êý×鵱ǰ³¤¶È
};


//
//ËÑË÷±ísearchList¼Ì³ÐÁËdataList£¬ ²¢ÇÒÔö¼ÓÁ˳ÉÔ±º¯ÊýSearch()
template <class Type> class searchList: public dataList<Type>
{
public:
searchList(int sz = 10): dataList <Type> (sz){} //¹¹Ô캯Êý

virtual ~searchList(){}

virtual int Search(const Type & x) const;
};


//
template <class Type> ostream & operator << (ostream & OutStream, const dataList<Type> & OutList)
{
OutStream << "Array Contents: \n"; //Êä³ö±íµÄËùÓбíÏîµ½OutStream
for (int i = 0; i < OutList.CurrentSize; i++)
{
OutStream << OutList.Element[i].getKey() << ' ';
}
OutStream << endl; //Êä³ö±íµÄµ±Ç°³¤¶Èµ½OutStream
OutStream << "Array Current Size:" << OutList.CurrentSize << endl;
return OutStream;
}

//
template <class Type> istream & operator >> (istream & InStream, dataList<Type> & InList)
{
cout << "Enter array CurrentSize:";
InStream >> InList.CurrentSize; //´ÓInstreamÊä³ö±íµÄµ±Ç°³¤¶È
cout << "Enter array elements:\n";
for (int i = 0; i < InList.CurrentSize; i++)
{
cout << "Element" << i << ":";
InStream >> InList.Element[i].key; //´ÓInstreamÊäÈë±íµÄÈ«²¿±íÏî
}
return InStream;
}

//
template <class Type> int searchList<Type>::Search(const Type & x) const
{
Element[0].setKey(x);
int i = CurrentSize;
while (Element[i].getKey() != x)
{
i--;
}
return i;
}
main.cpp

#include <iostream.h>
#include <stdlib.h>
#include "StaticSearch.h"

const int Size = 10;

int main()
{
searchList <float> List1(Size); //¶¨ÒåËÑË÷±íList1
float Target;
int Location;
cin >> List1;
cout << List1;
cout << "Search for a float:";
cin >> Target;
if ((Location = List1.Search(Target)) != 0)
{
cout << "Found at index" << Location << endl;
}
else
{
cout << "Not found. \n";
}

return 0;
}
测试通过
Dong 2004-09-28
  • 打赏
  • 举报
回复
上面有需要修改的地方,把
比如在operator<<里你使用了InStream >> InList.Element[i];
改为
比如在operator>>里你使用了InStream >> InList.Element[i];


哈哈。!
Dong 2004-09-28
  • 打赏
  • 举报
回复
上面有需要修改的地方,把
比如在operator<<里你使用了InStream >> InList.Element[i];
改为
比如在operator<<里你使用了InStream >> InList.Element[i];


慢了,被高手答了!我花时15分钟!看来还要提高自己
Dong 2004-09-28
  • 打赏
  • 举报
回复
Type *Element;换成Node<Type> *Element;
Type可不是一模板类型,因为你使用了Element(new Node<Type> [sz])是模板类型的

Instream--->InStream//小问题

最可看的就是下面的两个重载函数了
friend ostream &operator << (ostream &OutStream, const dataList <Type> & OutList);
friend istream &operator >> (istream &InStream, dataList <Type> & InList);
比如在operator<<里你使用了InStream >> InList.Element[i];你想想,他本身都没有定义好怎么可以调用啊??呵呵,这个出错就和在类的定义里面调用了他本身定义变量一样的错误(不是类指针哦)
你要先给定值然后再付给摸班类!呵呵!

不过不知道运行怎么!
insulator 2004-09-28
  • 打赏
  • 举报
回复
dataList(int sz = 10): Element(new Node <Type> [sz]), ArraySize(sz){}
这里有错误,你的Element是一个Type *而new Node <Type> [sz],是一个Node*
cxc014 2004-09-28
  • 打赏
  • 举报
回复
Type *Element; //数据表中存储数据的数组
==》Node<Type> *Element;
instream ==>inSteam;

InStream >> InList.Element[i]; //从Instream输入表的全部表项
//InList.Element[i]是个Node<Type>怎么能直接输入呢;
==》Type k;
for(int i=0;i<InList.CurrentSize;i++)
{
cout<<"Element"<<i<<":";
in>>k;
InList.Element[i].setKey(k);
}
同样,你输入时要加个getKey();
hetianxu 2004-09-28
  • 打赏
  • 举报
回复
是不是强制转换导致了问题?
tobeistodo1 2004-09-28
  • 打赏
  • 举报
回复
在线等
tobeistodo1 2004-09-28
  • 打赏
  • 举报
回复
上面给的贴号与这个基本一样的;
另外用的是Vc6.0调的
ddn_debugger 2004-09-28
  • 打赏
  • 举报
回复
这得看看程序吧,凭空不好乱讲的说。
tobeistodo1 2004-09-28
  • 打赏
  • 举报
回复
以下是 StaticSearch.h:
//
template <class Type> class dataList;

//
template <class Type> class Node //数据表中结点类的定义
{
friend class dataList<Type>;

public:

//下面几个尝试修改了几次!
// Node(const Type & value): key(value){} //构造函数
// Node(): key(0){}

Node(const Type &value)
{
// value = 0;
key = value;
}

Node(){}

Type getKey() const //读取关键码
{
return key;
}

void setKey(Type k) //修改关键码
{
key = k;
}

private:
Type key; //关键码域
Type other; //其他域
};

//
template <class Type> class dataList //数据表类定义
{
public:
dataList(int sz = 10): Element(new Node <Type> [sz]), ArraySize(sz){} //构造函数
/* dataList(int sz = 10)
{
int i;
ArraySize = sz;
// Element = new Type[sz];
Element = new Node <Type> [sz];
if (!Element)
{
cout << "Cannot allocate element.\n";
exit(1);
}

for (i = 0; i < sz; i++)
{
Element[i] = 0;
}
// CurrentSize = 0;
}
*/
virtual ~dataList()
{
delete [] Element;
}

//重载"<<" 和 ">>"
friend ostream &operator << (ostream &OutStream, const dataList <Type> & OutList);
friend istream &operator >> (istream &InStream, dataList <Type> & InList);

protected:
Type *Element; //数据表中存储数据的数组
int ArraySize; //数组最大长度
int CurrentSize; //数组当前长度
};


//
//搜索表searchList继承了dataList, 并且增加了成员函数Search()
template <class Type> class searchList: public dataList<Type>
{
public:
searchList(int sz = 10): dataList <Type> (sz){} //构造函数

virtual ~searchList(){}

virtual int Search(const Type & x) const;
};


//
template <class Type> ostream & operator << (ostream & OutStream, const dataList<Type> & OutList)
{
OutStream << "Array Contents: \n"; //输出表的所有表项到OutStream
for (int i = 0; i < OutList.CurrentSize; i++)
{
OutStream << OutList.Element[i] << ' ';
}
OutStream << endl; //输出表的当前长度到OutStream
OutStream << "Array Current Size:" << OutList.CurrentSize << endl;
return OutStream;
}

//
template <class Type> istream & operator >> (istream & InStream, dataList<Type> & InList)
{
cout << "Enter array CurrentSize:";
Instream >> InList.CurrentSize; //从Instream输出表的当前长度
cout << "Enter array elements:\n";
for (int i = 0; i < InList.CurrentSize; i++)
{
cout << "Element" << i << ":";
InStream >> InList.Element[i]; //从Instream输入表的全部表项
}
return InStream;
}

//
template <class Type> int searchList<Type>::Search(const Type & x) const
{
Element[0].setKey(x);
int i = CurrentSize;
while (Element[i].getKey() != x)
{
i--;
}
return i;
}


以下是main.cpp:

#include <iostream.h>
#include <stdlib.h>
#include "StaticSearch.h"

const int Size = 10;

int main()
{
searchList <float> List1(Size); //定义搜索表List1
float Target;
int Location;
cin >> List1;
cout << List1;
cout << "Search for a float:";
cin >> Target;
if ((Location = List1.Search(Target)) != 0)
{
cout << "Found at index" << Location << endl;
}
else
{
cout << "Not found. \n";
}

return 0;
}
oyd 2004-09-28
  • 打赏
  • 举报
回复
把你修改以后的直接贴出来吧

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧