A problem with the default constructor

G

Gary

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 << ' ';
}
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; //´ÓInstreamÊäÈë±íµÄÈ«²¿±íÏî
}
return InStream;
}

//
template <class Type> int searchList<Type>::Search(const Type & x) const
{
Element[0].setKey(x);
int i = CurrentSize;
while (Element.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!
 
J

John Harrison

Gary said:
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 << ' ';
}
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; //´ÓInstreamÊäÈë±íµÄÈ«²¿±íÏî
}
return InStream;
}

//
template <class Type> int searchList<Type>::Search(const Type & x) const
{
Element[0].setKey(x);
int i = CurrentSize;
while (Element.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 said:
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
 
Z

Zian Smith

Gary said:
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 said:
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 << ' ';
}
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; //´ÓInstreamÊäÈë±íµÄÈ«²¿±íÏî
}
return InStream;
}

//
template <class Type> int searchList<Type>::Search(const Type & x) const
{
Element[0].setKey(x);
int i = CurrentSize;
while (Element.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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,828
Latest member
LauraCastr

Latest Threads

Top