By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,002 Members | 1,275 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,002 IT Pros & Developers. It's quick & easy.

A problem with the default constructor

P: n/a
Hi,

I am a Chinese student, I have a problem with the following code

//The follwing code in StaticSearch.h:
//
template <class Type> class dataList;

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

public:
Node(const Type & value): key(value){} //构造函数

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): ArraySize(sz), Element(new Node <Type> [sz]){} //
构造函数

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;
}


//The following code in main.cpp:
#include <iostream.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;
}
I use the VC6.0 to program the code. The problem is printed like this:
--------------------Configuration: try3 - Win32 Debug--------------------
Compiling...
main.cpp
h:\programming\数据结构任务\try3\staticsearch.h(31 ) : error C2512:
'Node<float>' : no appropriate default constructor available
h:\programming\数据结构任务\try3\staticsearch.h(31 ) : while
compiling class-template member function '__thiscall
dataList<float>::dataList<float>(int)'
h:\programming\数据结构任务\try3\staticsearch.h(31 ) : fatal error C1903:
unable to recover from previous error(s); stopping compilation
h:\programming\数据结构任务\try3\staticsearch.h(31 ) : while
compiling class-template member function '__thiscall
dataList<float>::dataList<float>(int)'
Error executing cl.exe.

try3.exe - 2 error(s), 0 warning(s)
Please help me!
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Gary" <yr**********@hotmail.com> wrote in message
news:cj**********@mail.cn99.com...
Hi,

I am a Chinese student, I have a problem with the following code

//The follwing code in StaticSearch.h:
//
template <class Type> class dataList;

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

public:
Node(const Type & value): key(value){} //构造函数

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): ArraySize(sz), Element(new Node <Type> [sz]){} // 构造函数

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;
}


//The following code in main.cpp:
#include <iostream.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;
}
I use the VC6.0 to program the code. The problem is printed like this:
--------------------Configuration: try3 - Win32 Debug--------------------
Compiling...
main.cpp
h:\programming\数据结构任务\try3\staticsearch.h(31 ) : error C2512:
'Node<float>' : no appropriate default constructor available
h:\programming\数据结构任务\try3\staticsearch.h(31 ) : while
compiling class-template member function '__thiscall
dataList<float>::dataList<float>(int)'
h:\programming\数据结构任务\try3\staticsearch.h(31 ) : fatal error C1903:
unable to recover from previous error(s); stopping compilation
h:\programming\数据结构任务\try3\staticsearch.h(31 ) : while
compiling class-template member function '__thiscall
dataList<float>::dataList<float>(int)'
Error executing cl.exe.

try3.exe - 2 error(s), 0 warning(s)
Please help me!


You are trying to create an array of Node<Type>
dataList(int sz = 10): ArraySize(sz), Element(new Node <Type> [sz]){}

//

but Node<Type> does not have a default constructor. You must have a default
constructor to create any array.

The answer is very simple, add a default constructor to Node<Type>

template <class Type> class Node
{
friend class dataList<Type>;

public:
Node() {} // *** default constructor ***
Node(const Type & value): key(value){}

john
Jul 22 '05 #2

P: n/a
"Gary" <yr**********@hotmail.com> wrote in message news:<cj**********@mail.cn99.com>...
Hi,

I am a Chinese student, I have a problem with the following code

//The follwing code in StaticSearch.h:
//
template <class Type> class dataList;

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

public:
Node(const Type & value): key(value){} //构造函数
Need a default constructor here:
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): ArraySize(sz), Element(new Node <Type> [sz]){} //
构造函数

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

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

protected:
Type *Element; //数据表中存储数据的数组 Above line seems to be incorrect too, Element should be a pointer to a
'Node' datatype as per your consturctor in this class. Above line
should be:
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] << ' ';
}
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;
}


//The following code in main.cpp:
#include <iostream.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;
}
I use the VC6.0 to program the code. The problem is printed like this:
--------------------Configuration: try3 - Win32 Debug--------------------
Compiling...
main.cpp
h:\programming\数据结构任务\try3\staticsearch.h(31 ) : error C2512:
'Node<float>' : no appropriate default constructor available
h:\programming\数据结构任务\try3\staticsearch.h(31 ) : while
compiling class-template member function '__thiscall
dataList<float>::dataList<float>(int)'
h:\programming\数据结构任务\try3\staticsearch.h(31 ) : fatal error C1903:
unable to recover from previous error(s); stopping compilation
h:\programming\数据结构任务\try3\staticsearch.h(31 ) : while
compiling class-template member function '__thiscall
dataList<float>::dataList<float>(int)'
Error executing cl.exe.

try3.exe - 2 error(s), 0 warning(s)
Please help me!

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.