This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: exception on string (out_of_range)


On Thu, Jul 7, 2011 at 8:19 AM, eric <cneric12lin0@gmail.com> wrote:
> Dear Advanced C++ programers:
>
> ?from book (c++ cookbook), chapter 4, section 3: Storing Strings in a
> Sequence
> ---------
> #include <iostream>
> #include <vector>
> #include <exception>
>
> using namespace std;
>
> int main() {
>
> ?char carr[] = {'a', 'b', 'c', 'd', 'e'};
>
> ?cout << carr[100000] << '\n'; ? ?// Whoops, who knows what's going
> ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? // to happen
> ?vector<char> v;
> ?v.push_back('a');
> ?v.push_back('b');
> ?v.push_back('c');
> ?v.push_back('d');
> ?v.push_back('e');
>
> ?try {
> ? ? cout << v.at(10000) << '\n'; ?// at checks bounds and throws
> ?} catch(out_of_range& e) { ? ? ? // out_of_range if it's invalid
> ? ?cerr << e.what() << '\n';
> ?}
> }
> ----------------------------------------------------------------------
> my g++ response
> -----------------
> eric@eric-laptop:~/cppcookbook/download$ g++ 4-7.cpp
> 4-7.cpp: In function ‘int main()’:
> 4-7.cpp:20:11: error: expected type-specifier before ‘out_of_range’
> 4-7.cpp:20:23: error: expected ‘)’ before ‘&’ token
> 4-7.cpp:20:23: error: expected ‘{’ before ‘&’ token
> 4-7.cpp:20:25: error: ‘e’ was not declared in this scope
> 4-7.cpp:20:26: error: expected ‘;’ before ‘)’ token
> -------------------------------------------------------------------------------------
> the code can be download from
> http://examples.oreilly.com/9780596007614/
> that mean it was tested success in some platform(vc++ in xp), so
> please help, thanks a lot in advance, Eric
>

Eric -

So far all of your cookbook questions are answerable by a C++ forum,
of which gcc-help is not.  gcc-help is for questions regarding gcc
itself and not any particular programming language.  I could give you
the answer here, but I will not for fear that you will continue
posting questions here.

  Brian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]