arrays

This is a discussion on arrays within the C++ Programming forums, part of the General Programming Boards category; I wrote a program to do the following: Write a destructor to return the dynamic memory used by the list ...

  1. #1
    Unregistered
    Guest

    arrays

    I wrote a program to do the following:
    Write a destructor to return the dynamic memory used by the list to the heap.
    Write a function which returns the number of items in the list.
    Write a function which returns true if the list is full.
    Write a function to add one item to the list. The argument of the function contains the item to be added.
    Write a function to remove all of the items from the list.
    Write a function to print the contents of the list to a report.
    write a function to display the contents of the list to the display.
    i woul like to write a small program in main to test the array list.

    using namespace std;

    int main()
    {

    List a;
    a.add(1);


    return 0;

    }
    #include <iostream>
    #include<fstream>
    #include<string>
    #include<iomanip>

    List::List(int max)
    {
    int current_length=0;
    typedef int*item;

    item a;
    a = new int[max];
    if ( a==NULL)
    {
    cout<< "Error : Insufficient memory.\n";
    exit(1);
    }
    Max_number=max;



    }

    int List::length ()
    {

    return(current_length);

    }

    void List::add (int new_item)
    {
    cout<<"Enter a new item to the list: "<<endl;
    cin>>current_length;

    if(a.full())
    {
    cout<<"Error:adding to a full list."<<endl;
    exit(1);
    }
    else
    {

    a[current_length]=new_item;
    current_length=current_length+1;
    }

    }
    int List::full()
    {
    return(current_length==Max_number);
    }

    void List::erase ()
    {
    current_length=0;
    }


    List::~List()
    {

    delete[] a;
    }

    I get the following error messages:

    error C2512: 'List' : no appropriate default constructor available
    Error executing cl.exe.(in main.cpp)

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    a default constructor is a constructor put there when none other is availiabe. it's to maintain backwards compatability with C structs. when you provide any constructor, C++ won't do this for you.
    so, either you:
    1) called a constructor but forgot to put arguements in
    2) assumed wrongly that there was a default constructor

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function to read in two arrays
    By ssmokincamaro in forum C Programming
    Replies: 7
    Last Post: 11-12-2008, 07:59 AM
  2. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21