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! 2 3044
"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
"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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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();
//...
|
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...
|
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()
{
|
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...
|
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...
|
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...
|
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()
{
|
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...
|
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...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
| |