when i uncoment this line 144; i have error , and i debug and it was good, can anybody help me why i got this kind of error mybe should i declare List<employ>* list2, but it must wrok good like normale way List<employ> list2

    #include<iostream>
    #include<fstream>
    #include<string>

    using namespace std;


    class employ
    {
    private:
    int         m_id;
    string      m_name;
    float       m_salary; //m_ for memeber
    public:
    employ()
    {
        m_id     = 0;
        m_name   = "";
        m_salary = 0.0f;
    }
    ~employ()
    {} 

    void Input_Data()
    {
        cout << "Enter id: ";
        cin  >> m_id;
        cout << "Enter name: ";
        cin  >> m_name;
        cout << "Enter salary: ";
        cin  >> m_salary;
    }


    friend ostream& operator<<(ostream& show, const employ& emp)
    {
            show << "Id     =  " << emp.m_id     << endl;
            show << "Name   =  " << emp.m_name   << endl;
            show << "Salary =  " << emp.m_salary << endl;
            return(show);
        }

    } ;


    template<class TYPE>
    class Node
    {
    public:
        TYPE            m_Value;
        Node<TYPE>*      m_NextElement;  

    };

    template<class TYPE>
    class List
    {
    public:
        Node<TYPE>*      m_Start;
        Node<TYPE>*      m_Current;
        int             m_Size;

        List():m_Start(NULL),m_Current(NULL),m_Size(0)
        {
            m_Start     = new Node<TYPE>;
        }

        void Add(TYPE data)
        {
            if(Size() == 0) 
            {
                m_Start->m_Value = data;
                m_Start->m_NextElement   = new Node<TYPE>;
                m_Current                = m_Start->m_NextElement;
            }
            else
            {
                m_Current->m_Value       = data;
                m_Current->m_NextElement =  new Node<TYPE>;
                m_Current                =  m_Current->m_NextElement;       
            }
            m_Size++;

        }
        int Size()
        {
            return(m_Size);
        }
        void Print()
        {
            Node<TYPE>* temp = m_Start;
            while(temp != m_Current)
            {
                cout << temp->m_Value << endl;
                temp = temp->m_NextElement;
            }
        }

        void Save_To_File(string file_name)
        {
            ofstream Out(file_name.c_str(),ios_base::binary);
            Out.write( (char*)this,(sizeof(Node<employ>) * Size()));
            Out.close();
        }
        void Load_From_File(string file_name, int size)
        {
            if(size != 0)
            {
            m_Size = size;
            ifstream In(file_name.c_str(),ios_base::binary);
            In.read( (char*)this,sizeof(Node<employ>)*Size() );
            In.close();
            }
        }
        ~List()
        {
            Node<TYPE>* temp = m_Start;
            Node<TYPE>* del;
            while(temp != m_Current)
            {
                del = temp;
                temp = temp->m_NextElement;
                delete del;
            }
            m_Start = m_Current = 0;
        }
    };



    int main()
    { 
        List<employ> list1;
        employ tmp;
        char anwser = '?';
        do
        {
            tmp.Input_Data();
            list1.Add(tmp);
            cout << "Would You like to continue (Y/N) :";
            cin  >> anwser;
        }
        while(anwser == 'y' || anwser == 'Y');//all other key exit the loop
        //List<employ> list2;
        list1.Print();

    return 0;
    }

here to see good code:
Click Here
thank you

Recommended Answers

All 2 Replies

The problem is everywhere you new Node<TYPE>. The Node<Type> constructor does not set m_NextElement and everywhere you new it you don't set it either so in your list the final element in the list always has an invalid m_NextElement. When calling the list destructor this causes your program to exhibit undefined behaviour (in this case crash probably).

I would add a constructor to Node<Type> that initialised m_NextElement to NULL.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.