Exception Handling Help

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:

char MyString::at(int);
using::out_of_range; //(lol)
char at(int sub) const;
char& at(int sub) throw();
etc...

I just need to get this and my program is complete...

Inside of the MyString.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char MyString::at(int sub) const throw(out_of_range)
{
    if(sub < 0 || sub >= sSize){
        throw out_of_range("Subscript out of range");
    }
    else
    {
        return sArray[sub];
    }
}

char& MyString::at(int sub) throw(out_of_range)
{
    if(sub < 0 || sub >= sSize){
        throw out_of_range("Subscript out of range");
    }
    else
    {
        return sArray[sub];
    }
}


Inside of the MyString.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    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(const char* s);
        bool operator==(const MyString& rightOp) const;
        bool operator==(const char* rightOp) const;
        char operator[](int sub) const;
        char& operator[](int sub);
        const char* 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=(const char*);
        MyString operator+(const MyString&) const;
        MyString operator+(const char*) 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.
Last edited on
Damani,

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    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(const char* s);
        bool operator==(const MyString& rightOp) const;
        bool operator==(const char* rightOp) const;
        char operator[](int sub) const;
        char& operator[](int sub);
        const char* c_str() const;
        bool empty() const;
        int size() const;
        void clear();
        MyString(const MyString& s);
        char at(int sub) const throw(out_of_range);
        char& at(int sub) throw(out_of_range);
        int capacity() const;
        ~MyString();
        MyString& operator=(const MyString& rightOp);
        MyString& operator=(const char*);
        MyString operator+(const MyString&) const;
        MyString operator+(const char*) 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:

http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=109

Good Luck,
Matt
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:

using namespace 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).

Good Luck,
Matt
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

using namespace 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.
Topic archived. No new replies allowed.