2-D Vectors and Output?
Hi there - first post here on CProgramming, and until now I've managed to find solutions to my coding problems online or by thinking through it more. But here, I'm stumped:
My goal is to make, essentially, a 2-d grid of spaces or slots or something. At the moment, they are all filled with an integer called "label" that increments by one before every time it's put into a slot, so (hypothetically) space 0,0 is 1, space 0, 1 is 2, space 0, 2 is 3...etc.
Previously I tried using a 2d array for this but that failed pretty soundly since I wanted to define the length and width of it AFTER defining the array itself, and the compiler was not too agreeable on that.
Anyway, here's the source:
I realize I really -should- be using an iterator for this, but...I don't think this is my issue at the moment. If it is, please be kind enough to let me know so I can take a whack at that. :)
Oh wow, I suck at teh C++. I found at least one super-obvious problem after some more frustrated debugging...This post can probably be ignored - I would delete it, but I don't see an option...
Yep...it appears pretty much all of my problems have been solved: everything is running pretty much ok.
An example of CardboardAnalyst?
One thing I see is that in the constructor you shouldn't clear the row inside the second while loop, it should be outside the loop either before the loop or after the push_back.
Those while loops are perfect candidates to be for loops.
Now that you are using vectors, you don't need to keep track of the length, just use the length() function of the vector. This might have helped you diagnose the problem above.
That was my problem (the first thing you mentioned).
The reason I made those otherwise-for loops into WHILE loops is that for some reason, previously, when I used for() the program refused to output information from base[x][y]. I didn't understand why this was, but switched to while() because, quite frankly, it worked.
I'll try switching it back for simplicity's sake now.
EDIT: Yes -- when I go back to for() loops, the program won't display the output in the for loop and crashes after entering the coords to display...presumably because the vector values weren't assigned inside the for() loop with x & y? Strange indeed.
for (int x = 0; x < lenx; x++) should work.
Silly mistakes always make me feel silliest. Thank you again - it works fine when I swap around the parameters, and of course the code is cleaner.
I have yet another question that is somewhat related to the original topic, though - rather than clutter the board, I figured I'd just add it on here.
I've alread set up my vectors to accept Tile and it works just fine: the problem is that I can't figure out how to delete the Tile "typical" (the ~Tile you see there is about one of the 50 combinations I tried). I am creating and recreating a Tile to update a static counter so they all have unique id #s, to fill a motive...
If you define something by
How do you delete it?
It is automatically deleted when it goes out of scope. All variables created on the stack (Object instance;) are cleaned up automatically, you don't have to do anything.
When you add it to the vector, a copy is made in the vector and the one you created is cleaned up when it goes out of scope. The copy in the vector is deleted when the vector goes out of scope, or when the entry is removed from the vector due to a call to erase or clear.
Oh...so the business of deleting it is completely unnecessary! That's quite dandy, actually.
I (again) feel silly saying thank you (again), but it seems right.
Hopefully no more questions for a while.