473,795 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A problem with the default constructor

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; //Êý×鵱ǰ³¤¶È
};
//
//ËÑË÷±ísearchLis t¼Ì³ÐÁË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.Current Size; i++)
{
OutStream << OutList.Element[i] << ' ';
}
OutStream << endl; //Êä³ö±íµÄµ±Ç°³¤¶ ȵ½OutStream
OutStream << "Array Current Size:" << OutList.Current Size << endl;
return OutStream;
}

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

//
template <class Type> int searchList<Type >::Search(con st 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(Ta rget)) != 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\ Êý¾Ý½á¹¹ÈÎÎñ\tr y3\staticsearch .h(31) : error C2512:
'Node<float>' : no appropriate default constructor available
h:\programming\ Êý¾Ý½á¹¹ÈÎÎñ\tr y3\staticsearch .h(31) : while
compiling class-template member function '__thiscall
dataList<float> ::dataList<floa t>(int)'
h:\programming\ Êý¾Ý½á¹¹ÈÎÎñ\tr y3\staticsearch .h(31) : fatal error C1903:
unable to recover from previous error(s); stopping compilation
h:\programming\ Êý¾Ý½á¹¹ÈÎÎñ\tr y3\staticsearch .h(31) : while
compiling class-template member function '__thiscall
dataList<float> ::dataList<floa t>(int)'
Error executing cl.exe.

try3.exe - 2 error(s), 0 warning(s)
Please help me!
Jul 22 '05 #1
2 3076

"Gary" <yr**********@h otmail.com> wrote in message
news:cj******** **@mail.cn99.co m...
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; //Êý×鵱ǰ³¤¶È
};
//
//ËÑË÷±ísearchLis t¼Ì³ÐÁË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.Current Size; i++)
{
OutStream << OutList.Element[i] << ' ';
}
OutStream << endl; //Êä³ö±íµÄµ±Ç°³¤¶ ȵ½OutStream
OutStream << "Array Current Size:" << OutList.Current Size << endl;
return OutStream;
}

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

//
template <class Type> int searchList<Type >::Search(con st 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(Ta rget)) != 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\ Êý¾Ý½á¹¹ÈÎÎñ\tr y3\staticsearch .h(31) : error C2512:
'Node<float>' : no appropriate default constructor available
h:\programming\ Êý¾Ý½á¹¹ÈÎÎñ\tr y3\staticsearch .h(31) : while
compiling class-template member function '__thiscall
dataList<float> ::dataList<floa t>(int)'
h:\programming\ Êý¾Ý½á¹¹ÈÎÎñ\tr y3\staticsearch .h(31) : fatal error C1903:
unable to recover from previous error(s); stopping compilation
h:\programming\ Êý¾Ý½á¹¹ÈÎÎñ\tr y3\staticsearch .h(31) : while
compiling class-template member function '__thiscall
dataList<float> ::dataList<floa t>(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
"Gary" <yr**********@h otmail.com> wrote in message news:<cj******* ***@mail.cn99.c om>...
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; //Êý×鵱ǰ³¤¶È
};
//
//ËÑË÷±ísearchLis t¼Ì³ÐÁË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.Current Size; i++)
{
OutStream << OutList.Element[i] << ' ';
}
OutStream << endl; //Êä³ö±íµÄµ±Ç°³¤¶ ȵ½OutStream
OutStream << "Array Current Size:" << OutList.Current Size << endl;
return OutStream;
}

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

//
template <class Type> int searchList<Type >::Search(con st 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(Ta rget)) != 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\ Êý¾Ý½á¹¹ÈÎÎñ\tr y3\staticsearch .h(31) : error C2512:
'Node<float>' : no appropriate default constructor available
h:\programming\ Êý¾Ý½á¹¹ÈÎÎñ\tr y3\staticsearch .h(31) : while
compiling class-template member function '__thiscall
dataList<float> ::dataList<floa t>(int)'
h:\programming\ Êý¾Ý½á¹¹ÈÎÎñ\tr y3\staticsearch .h(31) : fatal error C1903:
unable to recover from previous error(s); stopping compilation
h:\programming\ Êý¾Ý½á¹¹ÈÎÎñ\tr y3\staticsearch .h(31) : while
compiling class-template member function '__thiscall
dataList<float> ::dataList<floa t>(int)'
Error executing cl.exe.

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

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
21202
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including pointers) and for objects types call their default constructor. Any others points i should know?
6
2043
by: Nafai | last post by:
Hello. I want to do something like this: class A { // It's virtual protected: float* data; int n; public: A(int a); virtual float* createData(); //...
18
3004
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a constructor that all parameters have default values
4
3136
by: Tapeesh | last post by:
Does C++ say that a default constructor needs to be generated by the compiler even if a class has the default constructor defined ? Say for eg. there is a class class A { int i; A() {
10
2408
by: Ook | last post by:
I'm having trouble comprehending what exactly "default construction" is. I know how to provide a constructor with initial values, so that if I, for example, in my code do this: MyClass zoot(1,2); It would instantiate MyClass, passing 1 and 2 to my constructor which in turn would do whatever I want it to. Would a default constructor be as follows:
11
2353
by: aaragon | last post by:
Hi everyone. I'm trying to write a class with policy based design (Alexandrescu's Modern C++ Design). I'm not a programmer but an engineer so this is kind of hard for me. Through the use of policies, I want to customize the structure of a class. The idea is to investigate the use of several data structures. One option would be the use of the boost dynamic bitset. Another would be the use of the std::vector. I obtained some code that...
74
16038
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
3
3774
by: Rahul | last post by:
Hi Everyone, I have the following code and the compiler complains that there isn't any default constructor available, class C { private: C() {
9
2903
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no Foo() is included, i believe. };
5
13384
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL http://mghospedagem.com/images/controlpanel.jpg instead of http://mghospedagem.comhttp://mghospedagem.com/images/controlpanel.jpg As u see, there's the website URL before the image URL.
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9519
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10164
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6780
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3727
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2920
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.