A problem with the default constructor

Discussion in 'C++' started by Gary, Sep 28, 2004.

  1. Gary

    Gary Guest

    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!
    Gary, Sep 28, 2004
    #1
    1. Advertising

  2. "Gary" <> wrote in message
    news:cjbfe5$v7m$99.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 << ' ';
    > }
    > 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<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
    John Harrison, Sep 28, 2004
    #2
    1. Advertising

  3. Gary

    Zian Smith Guest

    "Gary" <> wrote in message news:<cjbfe5$v7m$99.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 << ' ';
    > }
    > 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!
    Zian Smith, Sep 28, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Aire
    Replies:
    3
    Views:
    463
    Mike Wahler
    Jan 25, 2004
  2. Replies:
    9
    Views:
    959
    Alf P. Steinbach
    Mar 6, 2006
  3. Replies:
    4
    Views:
    415
    Salt_Peter
    Oct 12, 2006
  4. desktop
    Replies:
    2
    Views:
    458
    Victor Bazarov
    Jun 6, 2007
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,221
Loading...

Share This Page