Well, I have the code, I just don't have a clue how to declare it in the header file because my teacher is confusing. What I mean is how do I make a prototype for the two functions below? I've tried:
private:
char *sArray;
int sCapacity;
int sSize;
public:
// How do I declare the errors in here?? I'm supposed to use a using statement to do so
MyString();
MyString(constchar* s);
booloperator==(const MyString& rightOp) const;
booloperator==(constchar* rightOp) const;
charoperator[](int sub) const;
char& operator[](int sub);
constchar* c_str() const;
bool empty() const;
int size() const;
void clear();
MyString(const MyString& s);
int capacity() const;
~MyString();
MyString& operator=(const MyString& rightOp);
MyString& operator=(constchar*);
MyString operator+(const MyString&) const;
MyString operator+(constchar*) const;
};
Any help would be greatly appreciated. I don't think it's a super hard question :P it's just that my teachers take weeks to respond to questions... and their office hours are non existent. If any more code is needed, I'll be sure to post.
The header file snipit you posted does NOT include declarations for the two at methods defined in your implementation (.cpp) file. In any case the declaration lines you need are pretty much a cut-n-paste of the first line of the function implementation. You need only strip off the MyString:: and add a semicolon to the end. It should look like this:
private:
char *sArray;
int sCapacity;
int sSize;
public:
// How do I declare the errors in here?? I'm supposed to use a using statement to do so
MyString();
MyString(constchar* s);
booloperator==(const MyString& rightOp) const;
booloperator==(constchar* rightOp) const;
charoperator[](int sub) const;
char& operator[](int sub);
constchar* c_str() const;
bool empty() const;
int size() const;
void clear();
MyString(const MyString& s);
char at(int sub) constthrow(out_of_range);
char& at(int sub) throw(out_of_range);int capacity() const;
~MyString();
MyString& operator=(const MyString& rightOp);
MyString& operator=(constchar*);
MyString operator+(const MyString&) const;
MyString operator+(constchar*) const;
};
I should note that I've NEVER worked with anybody that's used the exception specification feature of C++. I think it's basically unused in practice. See this article:
Yea, my teacher gave us a hw assignment involving it, so I kinda have to go about it this way lol. For some reason, I had to remove the "out_of_range" text because it was asking for a type-specifier, but thank you for the help good sir and I will definitely give your link a read.
No, you shouldn't have to remove the out_of_range specifier. If you do, then I think you're missing the entire point of what your instructor is asking you to do. Don't you have a book or class notes or something with examples? I think you're in trouble in this class.
I suspect your problem is one (or both) of the following:
1) You didn't include the header file that declares the standard exception out_of_range:
#include <stdexcept>
2) The exception out_of_range exists in the namespace std. You either need to refer to this exception by it's fully qualified name (std::out_of_range) OR you need to add this statement after your includes:
usingnamespace std;
which tells the compiler to search the namespace "std" in addition to the global namespace when searching for symbols. In other words, when the compiler sees the symbol "out_of_range" in your code and can't find it's definition in the global namespace, the above statement will make the compiler also search for the symbol "out_of_range" in the namespace named "std" (where it will happily find it).
Well, I mean I replaced it with "std::out_of_range", and it works just fine. I dunno, I already had the #include <stdexcept>
and
usingnamespace std;
I'm not gonna lie, this year made a huge leap as far as difficulty. This is only my second year of programming. Perhaps I'm not studying well enough, but I got a 97% in my first year, not I'm at about a C... I'm trying to catch up, but I don't think I'm that bad where I don't know anything about header files and using things from the library.
But thanks again for your help. I was able to get everything in.