Yeah, I'm saying that there's something else. When I took out At, it still dumped after "Testing Overloaded <<:", but I'm not sure what the issue is.
Yeah, I'm saying that there's something else. When I took out At, it still dumped after "Testing Overloaded <<:", but I'm not sure what the issue is.
I'm saying that there isn't.
I'm not impressed. Taking code out does not magically fix anything.When I took out At, it still dumped after "Testing Overloaded <<:", but I'm not sure what the issue is.
If you would kindly read my earlier post, I have conclusively narrowed down the issue to line 99 which only doesn't work because you haven't implemented AddFront() and friends. I'm not saying that you should go and implement that too ... rather the takeaway is that your program works correctly up to line 99.
YOu have to remember that you are implementing the class slowly, focusing on passing one test at a time. Of course there are going to be functions that you haven't implemented causing core dumps. Stop execution before functions that you haven't implemented are tested.
My apologies, I misunderstood you. I thought you were check to see if the problem as At or something else. I wasn't aware that the core could dump unless I did something wrong with one of the existing codes and that including At had, somehow or another, changed one problem into a different problem. With that said, it seems like it's going to be hard to tell if the core dumping is the fault of the code or not. Since At seems to be OK I have moved on to GetFirst:
The code is dumping earlier than before...but then again it was dumping earlier with At than without, so I can't tell if this is OK or not:Code:std::string* DynamicStringArray::GetFirst() { if(currentsize == 0) { throw "Array Empty"; } return myarray[0]; }
You mentioned using a debugger. Could you explain more on this, please? I haven't heard of that before.Code:---------------------------------------- UNIT TEST: ---------------------------------------- *****Constructor***** PASSSED TEST 1 GetSize()! PASSSED TEST 2 GetCapacity()! PASSSED TEST 3 ToString()! PASSSED TEST 4 Empty()! PASSSED TEST 5 At(0) EXCEPTION HANDLING! Segmentation fault (core dumped)
Honestly I would just move on to the next test. Your professor wants to test more exception handling. The next test was for GetFront().
Your cygwin installation should have come with a gdb installation. At a prompt tryYou mentioned using a debugger. Could you explain more on this, please? I haven't heard of that before.
$ gdb --version
If you get output, that's your debugger. Try reading RMS's gdb Tutorial.
Question 1.1 "How do I compile with debugging symbols?" and part 4 "How do I use breakpoints?" are of particular importance.
I was about to ask why are you not throwing an exception of a class derived from std::exception, but then I saw in your instructor's test code:
I am appalled.Code:try { list.At(0); } catch (const string &e) { Test(e == "Invalid Location", "At(0) EXCEPTION HANDLING"); }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yeah, you're right, doing the tests in order was definitely better. After doing AddFront, I saw there were apparently over 40 tests. x.x GetFirst(12) was next, but when I added it, the core dumped again (still at 5). Is this a problem with the code?
Code:#include "dynamic_string_array.h" #include <iostream> #include <cmath> #include <string> #include <cctype> #include <sstream> using std::cin; using std::cout; using std::endl; using std::string; using std::stringstream; DynamicStringArray::DynamicStringArray(int size) : arraysize(size), currentsize(0) { myarray = new std::string* [arraysize](); } DynamicStringArray::~DynamicStringArray() { delete [] myarray; currentsize = 0; } std::string* DynamicStringArray::At(int i) const { if ((i < currentsize) && (i >= 0)) { return myarray[i]; } else { throw std::string("Invalid Location"); } } std::string* DynamicStringArray::GetFirst() { if(currentsize == 0) { throw "Array Empty"; } return myarray[0]; } std::string* DynamicStringArray::GetLast() { } void DynamicStringArray::AddFront(std::string* str) { if(currentsize == arraysize) { IncreaseCapacity(); } for (int i = currentsize; i > 0; i--) { myarray[i] = myarray[i - 1]; } myarray[0] = str; currentsize++; } void DynamicStringArray::AddBack(std::string* string) { } void DynamicStringArray::DeleteFront() { } void DynamicStringArray::DeleteBack() { } unsigned int DynamicStringArray::GetSize() const { return currentsize; } unsigned int DynamicStringArray::GetCapacity() const { return arraysize; } bool DynamicStringArray::Empty() const { return currentsize == 0; } void DynamicStringArray::Clear() { } void DynamicStringArray::Sort() { } std::string DynamicStringArray::ToString() { std::ostringstream ss; if (this->Empty() == false) { ss << *myarray[0]; for (unsigned int i = 1; i < currentsize; i++) { ss << ", " << *myarray[i]; } } return ss.str(); } std::ostream& operator<<(std::ostream& comma, const DynamicStringArray& DSA) { } void DynamicStringArray::IncreaseCapacity() { } std::string DynamicStringArray::ToUpper(std::string letter) { }
Yeah the teacher is really good at forcing some strange implementation decisions, isn't he?
Erik, please use std::exception when you aren't writing code for your teacher anymore.
Exceptions are caught by type. It's not specific, and that is why so many exceptions are their own type (std:ut_of_range, std::bad_alloc, etc.) Technically, a string exception would be caught by the first catch (string&) block the code runs into, which is not necessarily how you want to handle a specific error.Why's that?
From what I can tell, the only real problem is you are still throwing "strings like this" - that isn't actually caught by your teacher's unit test. A "string like this" is a char array, not a string. You should prefer to throw string("whatever"); for the purposes of the assignment.
Last edited by whiteflags; 04-06-2015 at 11:23 PM.
Ohhh OK, I see the difference now. It worked for GetFront, but not for GetLast--or rather there's something else going on there. It dumps after 14 now.
Code:#include "dynamic_string_array.h" #include <iostream> #include <cmath> #include <string> #include <cctype> #include <sstream> using std::cin; using std::cout; using std::endl; using std::string; using std::stringstream; DynamicStringArray::DynamicStringArray(int size) : arraysize(size), currentsize(0) { myarray = new std::string* [arraysize](); } DynamicStringArray::~DynamicStringArray() { delete [] myarray; currentsize = 0; } std::string* DynamicStringArray::At(int i) const { if ((i < currentsize) && (i >= 0)) { return myarray[i]; } else { throw std::string("Invalid Location"); } } std::string* DynamicStringArray::GetFirst() { if(currentsize == 0) { throw std::string("Array Empty"); } return myarray[0]; } std::string* DynamicStringArray::GetLast() { int i = currentsize; if(currentsize == 0) { throw std::string("Array Empty"); } return myarray[i]; } void DynamicStringArray::AddFront(std::string* str) { if(currentsize == arraysize) { IncreaseCapacity(); } for (int i = currentsize; i > 0; i--) { myarray[i] = myarray[i - 1]; } myarray[0] = str; currentsize++; } void DynamicStringArray::AddBack(std::string* string) { } void DynamicStringArray::DeleteFront() { } void DynamicStringArray::DeleteBack() { } unsigned int DynamicStringArray::GetSize() const { return currentsize; } unsigned int DynamicStringArray::GetCapacity() const { return arraysize; } bool DynamicStringArray::Empty() const { return currentsize == 0; } void DynamicStringArray::Clear() { } void DynamicStringArray::Sort() { } std::string DynamicStringArray::ToString() { std::ostringstream ss; if (this->Empty() == false) { ss << *myarray[0]; for (unsigned int i = 1; i < currentsize; i++) { ss << ", " << *myarray[i]; } } return ss.str(); } std::ostream& operator<<(std::ostream& comma, const DynamicStringArray& DSA) { } void DynamicStringArray::IncreaseCapacity() { } std::string DynamicStringArray::ToUpper(std::string letter) { }
This looks wrong:
I would expect:Code:std::string* DynamicStringArray::GetLast() { int i = currentsize; if(currentsize == 0) { throw std::string("Array Empty"); } return myarray[i]; }
Also, remember that these sort of member functions that do not change the logical/observable state of the object should be declared const so that they can be called from objects of this class that are const (or const references).Code:std::string* DynamicStringArray::GetLast() { if(currentsize == 0) { throw std::string("Array Empty"); } return myarray[currentsize - 1]; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Yeah, I can see that it's implemented wrong. I think you can figure it out though. Hint: when dealing with arrays, don't just automatically type i. The number between the brackets can be any expression - and what you want to return invariably is the last element of the array.Ohhh OK, I see the difference now. It worked for GetFront, but not for GetLast--or rather there's something else going on there.
Hey guys, I managed to get the code to pass all the tests. Thank you for your help and even more thanks for your patience. I know I'm not easy to work with, but I really do appreciate it. ^^;