boost library lexical_cast cann't be compiled

Jonathan Wakely jwakely.gcc@gmail.com
Mon Jul 4 10:21:00 GMT 2011


On 4 July 2011 10:07, eric wrote:
> Dear advanced g++ programers:
>
>  I tried to copy/test a piece simple boost library's lexical_cast
> c++ code from book c++Cookbook, page 122, example 3-2.
> ----------------------------------------------------------
> // Example 3-2. Using lexical_cast
>
> #include <iostream>
> #include <string>
> #include "boost/lexical_cast.hpp" /* I comple this program
>                                   * right above directory boost
>                                   */
>     //  <boost/lexical_cast.hpp>
>
> using namespace std;
>
> int main() {
>   string str1 = "750";
>   string str2 = "2.71";
>   string str3 = "0x7FFF";
>   try {
>     cout << boost::lexical_cast<int>(str1) << endl;
>     cout << boost::lexical_cast<double>(str2) << endl;
>     cout << boost::lexical_cast<int>(str3) << endl;
>   }
>   catch (boost::bad_lexical_cast& e) {
>     cerr << "Bad cast: " << e.what() << endl;
>   }
> }
> -----------------------------------------------------
> ---------------
>
> eric@eric-laptop:~/boost1/boost_1_46_1$ g++ usinglexicast.cpp
> usinglexicast.cpp: In function ‘int main()’:
> usinglexicast.cpp:16:34: error: ‘str3’ cannot appear in a
> constant-expression

'str3' does not appear on line 16 in the code above, and the code
above compiles fine, so I don't think it's what you're actually
compiling.

The error would imply you've written something like:

    cout << boost::lexical_cast<str3> << endl;

which is wrong, and not what you've pasted above.
Instead of asking for help as soon as you get an error why don't you
check you've copied the example code correctly.



More information about the Gcc-help mailing list