CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Out of bounds array problem

  1. #1
    Join Date
    Mar 2011

    Out of bounds array problem


    I am doing an exercise in which I need to do the following:

    Design and implement class myArray

    myArray class must solve the array index out of bounds problem and allow the user to begin the array index starting at any integer whether it be positive or negative.

    All objects of the class are of the type int.

    The program must terminate with an error message if the index is out of bounds during execution.

    The book makes me consider the following statements:

    myArray<int> list(5);
    myArray<int> myList(2, 13);
    myArray<int> yourList(-5, 9);

    which explains

    that list has 5 components ( list[0]-list[4] )
    myList has 11 components ( myList[2] - myList[12] )
    and yourList has 14 components (yourList[-5] - yourlist[9] ).

    It then wants me to write a program to test the class.

    I am not sure if it wants me to use list, myList, or yourList stated above in the program either so I was confused about that.

    I don't want this done for me, I just need a little help here and there, usually when the ball starts rolling, I can handle it, but this time I started with an error already than I can't figure out, so maybe some prying eyes can help me out! Here's what I have, keep in mind this is just incomplete code just to get something to get it compiling.

    //Class myArray
    #ifndef "H_myArray"
    #define "H_myArray"
    using namespace std;
    class myArray
                 myArray(int x);
                  int x;
    }//End myArray Class
    }//End myArray default constructor
    myArray::myArray(int x)
    }//End myArray constructor
    }//End myArray deconstructor
    #include <iostream>
    #include "myArray.h"
    using namespace std;
    int main()
        myArray arrayObject(5);
        return 0;   
    }//End main

    I keep getting these errors:

    Line 2 - In file included from myArray.cpp
    Line3:9 - macro names must be identifiers

    Line 8 - in Function int main(): 'myArray' undeclared (first use in this function)
    (Each undeclared identifier is reported only once for each function it appears in.)

    Line 8 - expected ';' before "arrayObject"
    [Build Error] [myArray.o] Error 1

    Can anyone clarify what mistake I made to get these errors before I start the program?

    I think this forum is extremely helpful, so thanks to all of you in advance for helping me in any way possible!

  2. #2
    Join Date
    Oct 2006

    Re: Out of bounds array problem

    The header guards should not be string literals. Remove the "".

    A class definition should always be terminated with a ;

    Also, never put a 'using namespace' in a header. That forfeits the intention with namespace's.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here

    Get your free MS compiler here

  3. #3
    Join Date
    Mar 2011

    Re: Out of bounds array problem

    Thanks for the input, it compiles beautifully now, time to get started! I will post back if I have any issues, I am not sure how I am going to attempt to complete the program but I have to do a little reading before hand and see what I can come up with and I will post my progress. Thanks so much!

  4. #4
    Join Date
    Aug 2009

    Re: Out of bounds array problem

    Just a little suggestion, the exercise sample arrays suggest your class should be a template, so i say you start with a template class from the beginning, so the code should look something like:
    template<typename T>
    class myArray
        myArray(unsigned arraySize);
        //...other c-tors - since you will need the "offset" if any
        //don't forget the copy c-tor and assignment operator
        //since you will have a pointer and you need to correctly copy the data
        T* data;
        unsigned size;

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)