cphipps (37)
OK, everything looks alright to me, but I'm pretty green when it comes to this. It is compiling and running fine, but it isn't outputting what I am expecting. Maybe I'm missing something? Thanks ahead of time for any help!
|
|
LB (13399)
Line 14 is illegal and should not compile.
Also, negative indicies are the memory before the array, which is undefined behavior to access. Most likely you are overwriting other variables.
cphipps (37)
Well it compiles and runs fine...I know negative indices are memory before the array. I know it can be done though, it's an assignment in my book. Maybe I'm just going about it the wrong way?
LB (13399)
cphipps wrote: |
---|
Well it compiles and runs fine... |
That's no indication of valid code. There are plenty of invalid programs that "compile and run fine" on some compilers.
Could you quote the exercise specifically? I have a feeling you're misunderstanding it (or maybe it is trying to show you what memory corruption can cause).
cphipps (37)
I know this also, I'm green, but it isn't my first rodeo. I was saying that because you said it shouldn't compile.
Here is the assignment though:
Recall that in C++, there is no check on an array index out of bounds. However, during program execution, an array index out of bounds can cause serious problems. Also, in C++, the array index starts at 0. Design and implement the class myArray that solves the array index out of bounds problem and also allows the user to begin the array index starting at any integer, positive or negative. Every object of type myArray is an array of type int. During execution, when accessing an array component, if the index is out of bounds, the program must terminate with an appro-priate error message. Consider the following statements: myArray<int> list(5); //Line 1 myArray<int> myList(2, 13); //Line 2 myArray<int> yourList(-5, 9); //Line 3 The statement in Line 1 declares list to be an array of 5 components, the component type is int, and the components are: list[0], list[1], ..., list[4]; the statement in Line 2 declares myList to be an array of 11 com-ponents, the component type is int, and the components are: myList[2], myList[3], ..., myList[12]; the statement in Line 3 declares yourList to be an array of 14 components, the component type is int, and the components are: yourList[-5], yourList[-4], ..., yourList[0], ..., yourList[8]. Write a program to test the class myArray.
LB (13399)
Ah, the assignment want you to make a class that allows for a different starting index than 0 and to disallow accessing out-of-bounds locations.
cphipps (37)
Yes...I guess I should've been more specific for the purpose of my program. I didn't quite fully understand it myself though. I have completely abandoned the above code though, and have started another way. I think I have it working fine, at the moment at least. Thanks for the assistance!
cphipps (37)
Edit:
False Alarm! I have figured it out! There were several errors just for the record. I just had to walk away for a bit.
OK, I abandoned my old code, and have started fresh.
Here is the error I'm getting:
114G:\USB Files\USB Files\Spring 2014\C++ Homework\Session 10\myArrayHeader.h[Error] expected unqualified-id before '{' token
Anyone see what I'm missing?
Thanks for any help!
Code:
|
|
|
|
|
|
Last edited on
LB (13399)
WHat are lines 11 to 24 supposed to be for in your header? They're not attached to anything and they won't work anyway.