Thread: Any idea?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    Question Any idea?

    I am getting this compile error when I run the below program:

    Error : illegal struct/union/enum/class definition
    bubbleSort.cpp line 15 double[] a;

    Error : declaration syntax error
    bubbleSort.cpp line 17 };

    Error : undefined identifier 'a'
    bubbleSort.cpp line 21 a = new double[max];

    Error : undefined identifier 'a'
    bubbleSort.cpp line 27 a[nElems] = value;

    Error : undefined identifier 'a'
    bubbleSort.cpp line 35 cout << a[j]+ " ";

    Error : undefined identifier 'a'
    bubbleSort.cpp line 44 if(a[in]>a[in+1])

    Error : undefined identifier 'a'
    bubbleSort.cpp line 50 double temp = a[one];

    Error : undefined identifier 'a'
    bubbleSort.cpp line 51 a[one] = a[two];

    Error : undefined identifier 'a'
    bubbleSort.cpp line 52 a[two] = temp;

    Error : function call 'ArrayBub()' does not match
    'ArrayBub::ArrayBub(int)'
    'ArrayBub::ArrayBub(const ArrayBub &)'
    bubbleSort.cpp line 58 ArrayBub arr;

    Error : illegal operand
    bubbleSort.cpp line 59 arr = new ArrayBub(maxSize);

    Error : illegal operand
    bubbleSort.cpp line 75 cout<< arr.display();

    Error : illegal operand
    bubbleSort.cpp line 79 cout << arr.display();


    Your insights are appreciated, thanks



    #include <iostream.h>

    class ArrayBub
    {
    public:
    ArrayBub(int);
    void insert(double);
    void display();
    void bubbleSort();
    void swap(int, int);

    private:
    double[] a;
    int nElems;
    };

    ArrayBub::ArrayBub(int max)
    {
    a = new double[max];
    nElems = 0;
    }

    void ArrayBub::insert(double value)
    {
    a[nElems] = value;
    nElems++;
    }

    void ArrayBub::display()
    {
    for(int j=0; j<nElems; j++)
    {
    cout << a[j]+ " ";
    }
    }

    void ArrayBub::bubbleSort()
    {
    int out, in;
    for (out=nElems-1; out>1; out--)
    for(in=0; in<out; in++)
    if(a[in]>a[in+1])
    swap(in, in+1);
    }

    void ArrayBub::swap(int one, int two)
    {
    double temp = a[one];
    a[one] = a[two];
    a[two] = temp;
    }

    int main()
    {
    int maxSize = 100;
    ArrayBub arr;
    arr = new ArrayBub(maxSize);

    arr.insert(2213);
    arr.insert(33);
    arr.insert(2);
    arr.insert(58);
    arr.insert(987);
    arr.insert(11);
    arr.insert(47);
    arr.insert(7);
    arr.insert(223);
    arr.insert(26);
    arr.insert(58);
    arr.insert(7);


    cout<< arr.display();

    arr.bubbleSort();

    cout << arr.display();



    return 0;
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    double[] a;
    You have to specify a size, ie: double a[12345];
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

  4. #4
    looking for the truth moemen ahmed's Avatar
    Join Date
    Feb 2002
    Location
    Egypt
    Posts
    161
    try this code
    and repost to tell us if it works like you expect , please

    IMPORTANT NOTICE: operator new() return a pointer , you should deal with it in this way


    Code:
    #include <iostream.h>
    #include <conio.h>
    class ArrayBub
    {
    public:
    ArrayBub(int);
    void insert(double);
    void display();
    void bubbleSort();
    void swap(int, int);
    
    private:
    double* a ;
    int nElems;
    };
    
    ArrayBub::ArrayBub(int max)
    {
    a = new double[max];
    nElems = 0;
    }
    
    void ArrayBub::insert(double value)
    {
    a[nElems] = value;
    nElems++;
    }
    
    void ArrayBub::display()
    {
    for(int j=0; j<nElems; j++)
    {
    cout << *(a)<< " ";
    a++;
    }
    cout<<endl;
    }
    
    void ArrayBub::bubbleSort()
    {
    int out, in;
    for (out=nElems-1; out>1; out--)
    for(in=0; in<out; in++)
    if(a[in]>a[in+1])
    swap(in, in+1);
    }
    
    void ArrayBub::swap(int one, int two)
    {
    double temp = a[one];
    a[one] = a[two];
    a[two] = temp;
    }
    
    int main()
    {
    int maxSize = 100;
    ArrayBub *arr;
    arr = new ArrayBub(maxSize);
    
    arr->insert(2213);
    arr->insert(33);
    arr->insert(2);
    arr->insert(58);
    arr->insert(987);
    arr->insert(11);
    arr->insert(47);
    arr->insert(7);
    arr->insert(223);
    arr->insert(26);
    arr->insert(58);
    arr->insert(7);
    
    arr->display();
    arr->bubbleSort();
    arr->display();
    
    
    getche();
    return 0;
    }

    i hope it helps
    pointer
    Programming is a high logical enjoyable art for both programer and user !!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. project idea ???
    By gemini_shooter in forum C Programming
    Replies: 2
    Last Post: 06-09-2005, 09:56 AM
  2. Have an idea?
    By B0bDole in forum Projects and Job Recruitment
    Replies: 46
    Last Post: 01-13-2005, 03:25 PM
  3. A little problem about an idea, help please
    By louis_mine in forum C++ Programming
    Replies: 3
    Last Post: 09-10-2004, 09:52 PM
  4. totally screwed up idea
    By iain in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 08-17-2001, 12:09 PM