Problem with std::out_of_range

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • desktop

    Problem with std::out_of_range

    I have the following code:

    #include <stdexcept>


    int main(){

    int i = 10;
    int arr[i];
    int index = 11;

    if (index i) {
    throw std::out_of_ran ge();
    }
    else {
    arr[index] = 33;
    }

    return 0;

    }

    but when I compile I get:

    no matching function for call to ‘std::out_of_ra nge::out_of_ran ge()’
    /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:99:
    note: candidates are: std::out_of_ran ge::out_of_rang e(const std::string&)
    /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:97:
    note: std::out_of_ran ge::out_of_rang e(const
    std::out_of_ran ge&)


    why can't I use std::out_of_ran ge() in this manner?
  • Alf P. Steinbach

    #2
    Re: Problem with std::out_of_ran ge

    * desktop:
    I have the following code:
    >
    #include <stdexcept>
    >
    int main(){
    >
    int i = 10;
    int arr[i];
    Not standard C++.

    int index = 11;
    >
    if (index i) {
    throw std::out_of_ran ge();
    }
    else {
    arr[index] = 33;
    }
    >
    return 0;
    Not necessary: main is special, check out the various special rules for
    main.

    }
    >
    but when I compile I get:
    >
    no matching function for call to ‘std::out_of_ra nge::out_of_ran ge()’
    /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:99:
    note: candidates are: std::out_of_ran ge::out_of_rang e(const std::string&)
    /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:97:
    note: std::out_of_ran ge::out_of_rang e(const
    std::out_of_ran ge&)
    >
    >
    why can't I use std::out_of_ran ge() in this manner?
    The error messages tell you (1) there is no default constructor, the one
    you attempted to use, (2) but there is a constructor taking std::string
    argument, and (3) there is a copy constructor.

    Why don't you just /read/ the error messages?


    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Baltimore

      #3
      Re: Problem with std::out_of_ran ge

      On May 21, 6:44 pm, desktop <f...@sss.comwr ote:
      I have the following code:
      >
      #include <stdexcept>
      >
      int main(){
      >
      int i = 10;
      int arr[i];
      int index = 11;
      >
      if (index i) {
      throw std::out_of_ran ge();
      }
      else {
      arr[index] = 33;
      }
      >
      return 0;
      >
      }
      >
      but when I compile I get:
      >
      no matching function for call to 'std::out_of_ra nge::out_of_ran ge()'
      /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:99:
      note: candidates are: std::out_of_ran ge::out_of_rang e(const std::string&)
      /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:97:
      note: std::out_of_ran ge::out_of_rang e(const
      std::out_of_ran ge&)
      >
      why can't I use std::out_of_ran ge() in this manner?
      The static method std::out_of_ran ge doesn't exist without arguments,
      but with a std::string to indicate a message to display when the
      exception is thrown.
      e.g. : terminate called after throwing an instance of
      'std::out_of_ra nge'
      what(): My message

      Comment

      • Rolf Magnus

        #4
        Re: Problem with std::out_of_ran ge

        desktop wrote:
        I have the following code:
        >
        #include <stdexcept>
        >
        >
        int main(){
        >
        int i = 10;
        int arr[i];
        int index = 11;
        >
        if (index i) {
        throw std::out_of_ran ge();
        }
        else {
        arr[index] = 33;
        }
        >
        return 0;
        >
        }
        >
        >
        why can't I use std::out_of_ran ge() in this manner?
        Because there is
        no matching function for call to ‘std::out_of_ range::out_of_r ange()’
        You should
        note: candidates are: std::out_of_ran ge::out_of_rang e(const std::string&)

        Comment

        • Salt_Peter

          #5
          Re: Problem with std::out_of_ran ge

          On May 21, 12:44 pm, desktop <f...@sss.comwr ote:
          I have the following code:
          >
          #include <stdexcept>
          >
          int main(){
          >
          int i = 10;
          int arr[i];
          int index = 11;
          >
          if (index i) {
          throw std::out_of_ran ge();
          }
          else {
          arr[index] = 33;
          }
          >
          return 0;
          >
          }
          >
          but when I compile I get:
          >
          no matching function for call to 'std::out_of_ra nge::out_of_ran ge()'
          /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:99:
          note: candidates are: std::out_of_ran ge::out_of_rang e(const std::string&)
          /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:97:
          note: std::out_of_ran ge::out_of_rang e(const
          std::out_of_ran ge&)
          >
          why can't I use std::out_of_ran ge() in this manner?

          First off, your logic is wrong since arr[10] doesn't exist either. If
          you are going to use exceptions and not bother catching them, you are
          waisting your time.

          #include <iostream>
          #include <stdexcept>

          template< typename T, const size_t Size >
          class Array
          {
          T m_array[Size];
          public:
          T& operator[](const size_t i)
          {
          if(i >= Size)
          throw std::out_of_ran ge("index out of range.");
          return m_array[i];
          }
          };

          int main()
          {
          try {
          Array< int, 10 instance;
          instance[10] = 99; // throws
          }
          catch (const std::exception& e)
          {
          std::cout << "Error: " << e.what();
          std::cout << std::endl;
          }
          }

          And instead of reinventing the wheel, why don't you use std::vector
          and its at() member function?


          Comment

          • desktop

            #6
            Re: Problem with std::out_of_ran ge

            Salt_Peter wrote:
            On May 21, 12:44 pm, desktop <f...@sss.comwr ote:
            >I have the following code:
            >>
            >#include <stdexcept>
            >>
            >int main(){
            >>
            > int i = 10;
            > int arr[i];
            > int index = 11;
            >>
            > if (index i) {
            > throw std::out_of_ran ge();
            > }
            > else {
            > arr[index] = 33;
            > }
            >>
            > return 0;
            >>
            >}
            >>
            >but when I compile I get:
            >>
            > no matching function for call to 'std::out_of_ra nge::out_of_ran ge()'
            >/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:99:
            >note: candidates are: std::out_of_ran ge::out_of_rang e(const std::string&)
            >/usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:97:
            >note: std::out_of_ran ge::out_of_rang e(const
            >std::out_of_ra nge&)
            >>
            >why can't I use std::out_of_ran ge() in this manner?
            >
            >
            First off, your logic is wrong since arr[10] doesn't exist either.
            What do you mean? Is it not just a a regular int array with 10 integers?


            If
            you are going to use exceptions and not bother catching them, you are
            waisting your time.
            >
            Well does that not depend on what you want?

            If I use a try/catch its because I want the program to continue after
            the exception has been thrown. But sometimes I just want the program to
            terminate and therefore I avoid using try/catch and just use a throw
            statement.

            Comment

            • Marcus Kwok

              #7
              Re: Problem with std::out_of_ran ge

              desktop <fff@sss.comwro te:
              Salt_Peter wrote:
              >On May 21, 12:44 pm, desktop <f...@sss.comwr ote:
              >>I have the following code:
              >>>
              >>#include <stdexcept>
              >>>
              >>int main(){
              >>>
              >> int i = 10;
              >> int arr[i];
              >> int index = 11;
              >>>
              >> if (index i) {
              >> throw std::out_of_ran ge();
              >> }
              >> else {
              >> arr[index] = 33;
              >> }
              >>>
              >> return 0;
              >>>
              >>}
              >>
              >First off, your logic is wrong since arr[10] doesn't exist either.
              >
              What do you mean? Is it not just a a regular int array with 10 integers?
              arr has 10 elements, but the indices go from arr[0] to arr[9].
              Therefore, arr[10] does not exist. In your code, if index == 10, then
              it will try to assign to arr[10], which is an error.

              --
              Marcus Kwok
              Replace 'invalid' with 'net' to reply

              Comment

              • Alf P. Steinbach

                #8
                Re: Problem with std::out_of_ran ge

                * Marcus Kwok:
                desktop <fff@sss.comwro te:
                >Salt_Peter wrote:
                >>On May 21, 12:44 pm, desktop <f...@sss.comwr ote:
                >>>I have the following code:
                >>>>
                >>>#include <stdexcept>
                >>>>
                >>>int main(){
                >>>>
                >>> int i = 10;
                >>> int arr[i];
                >>> int index = 11;
                >>>>
                >>> if (index i) {
                >>> throw std::out_of_ran ge();
                >>> }
                >>> else {
                >>> arr[index] = 33;
                >>> }
                >>>>
                >>> return 0;
                >>>>
                >>>}
                >>First off, your logic is wrong since arr[10] doesn't exist either.
                >What do you mean? Is it not just a a regular int array with 10 integers?
                >
                arr has 10 elements, but the indices go from arr[0] to arr[9].
                Therefore, arr[10] does not exist. In your code, if index == 10, then
                it will try to assign to arr[10], which is an error.
                arr doesn't have 10 elements in standard C++.

                It doesn't exist.

                --
                A: Because it messes up the order in which people normally read text.
                Q: Why is it such a bad thing?
                A: Top-posting.
                Q: What is the most annoying thing on usenet and in e-mail?

                Comment

                • BobR

                  #9
                  Re: Problem with std::out_of_ran ge


                  desktop <fff@sss.comwro te in message ...
                  I have the following code:
                  >
                  #include <stdexcept>
                  int main(){
                  // int i = 10;

                  int const i( 10 );
                  int arr[i];
                  int index = 11;
                  >
                  if (index i) {
                  // throw std::out_of_ran ge();

                  std::string msg( "out of range error in main()" );
                  throw std::out_of_ran ge( msg );
                  }
                  else {
                  arr[index] = 33;
                  }
                  >
                  return 0;
                  }
                  >
                  but when I compile I get:
                  <snip>
                  >
                  why can't I use std::out_of_ran ge() in this manner?
                  Why would you want to? Where are you going to 'catch' it?
                  Why not 'throw std::terminate( );' or 'return EXIT_FAILURE;' ?

                  --
                  Bob R
                  POVrookie


                  Comment

                  • Pete Becker

                    #10
                    Re: Problem with std::out_of_ran ge

                    BobR wrote:
                    std::string msg( "out of range error in main()" );
                    throw std::out_of_ran ge( msg );
                    Simpler:

                    throw std::out_of_ran ge("out of range error in main()");

                    --

                    -- Pete
                    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
                    Author of "The Standard C++ Library Extensions: a Tutorial and
                    Reference." (www.petebecker.com/tr1book)

                    Comment

                    • Marcus Kwok

                      #11
                      Re: Problem with std::out_of_ran ge

                      Alf P. Steinbach <alfps@start.no wrote:
                      * Marcus Kwok:
                      >desktop <fff@sss.comwro te:
                      >>Salt_Peter wrote:
                      >>>On May 21, 12:44 pm, desktop <f...@sss.comwr ote:
                      >>>>I have the following code:
                      >>>>>
                      >>>>#include <stdexcept>
                      >>>>>
                      >>>>int main(){
                      >>>>>
                      >>>> int i = 10;
                      >>>> int arr[i];
                      >>>> int index = 11;
                      >>>>>
                      >>>> if (index i) {
                      >>>> throw std::out_of_ran ge();
                      >>>> }
                      >>>> else {
                      >>>> arr[index] = 33;
                      >>>> }
                      >>>>>
                      >>>> return 0;
                      >>>>>
                      >>>>}
                      >>>First off, your logic is wrong since arr[10] doesn't exist either.
                      >>What do you mean? Is it not just a a regular int array with 10 integers?
                      >>
                      >arr has 10 elements, but the indices go from arr[0] to arr[9].
                      >Therefore, arr[10] does not exist. In your code, if index == 10, then
                      >it will try to assign to arr[10], which is an error.
                      >
                      arr doesn't have 10 elements in standard C++.
                      >
                      It doesn't exist.
                      Oh, right, I overlooked that i was not const, which you had already
                      pointed out in a previous post, but then I got distracted by the
                      indexing problem :)

                      --
                      Marcus Kwok
                      Replace 'invalid' with 'net' to reply

                      Comment

                      • Salt_Peter

                        #12
                        Re: Problem with std::out_of_ran ge

                        On May 21, 2:08 pm, desktop <f...@sss.comwr ote:
                        Salt_Peter wrote:
                        On May 21, 12:44 pm, desktop <f...@sss.comwr ote:
                        I have the following code:
                        >
                        #include <stdexcept>
                        >
                        int main(){
                        >
                        int i = 10;
                        int arr[i];
                        int index = 11;
                        >
                        if (index i) {
                        throw std::out_of_ran ge();
                        }
                        else {
                        arr[index] = 33;
                        }
                        >
                        return 0;
                        >
                        }
                        >
                        but when I compile I get:
                        >
                        no matching function for call to 'std::out_of_ra nge::out_of_ran ge()'
                        /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:99:
                        note: candidates are: std::out_of_ran ge::out_of_rang e(const std::string&)
                        /usr/lib/gcc/i486-linux-gnu/4.1.2/../../../../include/c++/4.1.2/stdexcept:97:
                        note: std::out_of_ran ge::out_of_rang e(const
                        std::out_of_ran ge&)
                        >
                        why can't I use std::out_of_ran ge() in this manner?
                        >
                        First off, your logic is wrong since arr[10] doesn't exist either.
                        >
                        What do you mean? Is it not just a a regular int array with 10 integers?
                        It would be if the array's size is const.
                        An array with 10 elements runs from 0 to 9.
                        There is no arr[10].

                        Think:
                        arr[0] arr + 0 is first element (think offset)
                        arr[1] arr + 1 is second element
                        ....
                        arr[9] arr + 9 is tenth element

                        C++ doesn't do like in VB where an array of 10 elements has 11
                        elements but the first one at array[0] is left unused. Whoever thought
                        that up was brain-dead and didn't know how to count.
                        >
                        If
                        >
                        you are going to use exceptions and not bother catching them, you are
                        waisting your time.
                        >
                        Well does that not depend on what you want?
                        >
                        If I use a try/catch its because I want the program to continue after
                        the exception has been thrown. But sometimes I just want the program to
                        terminate and therefore I avoid using try/catch and just use a throw
                        statement.
                        Thats illogical. You are forcing the program to commit an unhandled
                        exception.
                        Lets face it, how where you planning to figure out that an
                        out_of_range exception was indeed thrown? Specially since you seem to
                        indicate that arr[10] exists (it doesn't)?
                        The point here is that you are throwing an out_of_range exception, are
                        you not?
                        Why are you splitting your brain to throw a particular type of
                        exception and then not bother catching it for feedback?
                        Why not return or terminate instead? Or throw an integer?




                        Comment

                        • James Kanze

                          #13
                          Re: Problem with std::out_of_ran ge

                          On May 21, 8:08 pm, desktop <f...@sss.comwr ote:
                          Salt_Peter wrote:
                          [...]
                          If
                          you are going to use exceptions and not bother catching them, you are
                          waisting your time.
                          Well does that not depend on what you want?
                          If I use a try/catch its because I want the program to continue after
                          the exception has been thrown. But sometimes I just want the program to
                          terminate and therefore I avoid using try/catch and just use a throw
                          statement.
                          In which case, abort() (or assert(0), to get line number and
                          file name) is generally better. When the program aborts because
                          of an uncaught exception, it's undefined whether the stack has
                          been unwound and the context lost or not.

                          --
                          James Kanze (GABI Software) email:james.kan ze@gmail.com
                          Conseils en informatique orientée objet/
                          Beratung in objektorientier ter Datenverarbeitu ng
                          9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                          Comment

                          • Nick Keighley

                            #14
                            Re: Problem with std::out_of_ran ge

                            On 22 May, 05:01, Salt_Peter <pj_h...@yahoo. comwrote:
                            On May 21, 2:08 pm, desktop <f...@sss.comwr ote:
                            Salt_Peter wrote:
                            On May 21, 12:44 pm, desktop <f...@sss.comwr ote:
                            >I have the following code:
                            >
                            >#include <stdexcept>
                            >
                            >int main(){
                            >
                            > int i = 10;
                            > int arr[i];
                            > int index = 11;
                            >
                            > if (index i) {
                            > throw std::out_of_ran ge();
                            > }
                            > else {
                            > arr[index] = 33;
                            > }
                            >
                            > return 0;
                            >
                            >}
                            >
                            >but when I compile I get:
                            <various errors>
                            <snip>
                            If
                            you are going to use exceptions and not bother catching them, you are
                            waisting your time.
                            >
                            Well does that not depend on what you want?
                            well, I'm not an experienced C++ user, but I do sometimes throw
                            exceptions that arn't caught...

                            If I use a try/catch its because I want the program to continue after
                            the exception has been thrown. But sometimes I just want the program to
                            terminate and therefore I avoid using try/catch and just use a throw
                            statement.
                            >
                            Thats illogical. You are forcing the program to commit an unhandled
                            exception.
                            so? Is that wrong?

                            Lets face it, how where you planning to figure out that an
                            out_of_range exception was indeed thrown? Specially since you seem to
                            indicate that arr[10] exists (it doesn't)?
                            The point here is that you are throwing an out_of_range exception, are
                            you not?
                            and the program can't handle it, so it terminates. So?

                            Why are you splitting your brain to throw a particular type of
                            exception and then not bother catching it for feedback?
                            Why not return or terminate instead?
                            if you terminates then the destructors don't get called.
                            In a larger program that might clean up and release resources
                            that are important. Returning means the program is running in an
                            inconsistant state.

                            Or throw an integer?
                            Effective C++ (or one of its children) recomends that all exceptions
                            are derived from std::exception. I have a project that has six (six!)
                            different exception base classes. This is a pain.

                            Suppose the throw std::out_of_ran ge was in a library. The original
                            client cannot handle out of range (eg. Ariane-4 :-) ). A later
                            client must handle the exception (call it Ariane-5 for example)
                            but cannot recompile the library. Now std::out_of_ran ge makes sense.

                            Normally I catch exceptions in main() so at least it reports the
                            exception before it terminates. But all the destructors get called
                            first.


                            --
                            Nick Keighley



                            Comment

                            • Rolf Magnus

                              #15
                              Re: Problem with std::out_of_ran ge

                              James Kanze wrote:
                              On May 21, 8:08 pm, desktop <f...@sss.comwr ote:
                              >Salt_Peter wrote:
                              >
                              [...]
                              If
                              you are going to use exceptions and not bother catching them, you are
                              waisting your time.
                              >
                              >Well does that not depend on what you want?
                              >
                              >If I use a try/catch its because I want the program to continue after
                              >the exception has been thrown. But sometimes I just want the program to
                              >terminate and therefore I avoid using try/catch and just use a throw
                              >statement.
                              >
                              In which case, abort() (or assert(0), to get line number and
                              file name) is generally better.
                              I get a full backtrace from unhandled exceptions.
                              When the program aborts because of an uncaught exception, it's undefined
                              whether the stack has been unwound and the context lost or not.
                              And that's different for abort()?

                              Comment

                              Working...