[C++] strange problem with std::ostringstream

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

    [C++] strange problem with std::ostringstream

    Hi ! I have a strange problem with a std::ostringstr eam..

    code :
    #include <sstream>

    /*...*/

    std::ostringstr eam ss();
    ss << "\"\"" << libpath << "\"\" \"\"" << argfilename << "\"\" \"\""
    << outfilename << "\"\""; //line 75

    std::string callstring = ss.str(); //line 77

    /* ... */

    I don't have a undeclared identifier error when declaring std::ostringstr eam
    ss();
    however, I have thoses stranges errors :

    c:\documents and settings\eric\m es documents\progr amming\stdcpp dynamic
    code\loadlib.hp p(75) : error C2296: '<<' : illegal, left operand has type
    'class std::basic_ostr ingstream<char, struct std::char_trait s<char>,class
    std::allocator< char> > (__cdec
    l *)(void)'
    c:\documents and settings\eric\m es documents\progr amming\stdcpp dynamic
    code\loadlib.hp p(75) : error C2297: '<<' : illegal, right operand has type
    'char [3]'
    c:\documents and settings\eric\m es documents\progr amming\stdcpp dynamic
    code\loadlib.hp p(77) : error C2228: left of '.str' must have
    class/struct/union type


    I use vc++ 6.0

    that's a very strange error and I really do not have a clue how to solve
    it.. I mean.. ss is not a undeclared identifier.. and it's really defined
    as a std::ostringstr eam.. but I have thoses errors...


    any help appreciated.. really !!

    thanks !

    -Eric Boutin


  • Rob Williscroft

    #2
    Re: [C++] strange problem with std::ostringstr eam

    Eric Boutin wrote in news:t_czb.1244 12$Ac1.2065931@ weber.videotron .net:

    This:
    [color=blue]
    > std::ostringstr eam ss();
    >[/color]

    is a function declaration

    std::ostringstr eam ss( void );

    change it to

    std::ostringstr eam ss;

    Rob.
    --

    Comment

    • Eric Boutin

      #3
      Re: [C++] strange problem with std::ostringstr eam

      This is a normal behavior or a behavior from a crappy compiler ?

      "Rob Williscroft" <rtw@freenet.RE MOVE.co.uk> a écrit dans le message de
      news:Xns9446275 BB2EFEukcoREMOV Efreenetrtw@195 .129.110.130...[color=blue]
      > Eric Boutin wrote in news:t_czb.1244 12$Ac1.2065931@ weber.videotron .net:
      >
      > This:
      >[color=green]
      > > std::ostringstr eam ss();
      > >[/color]
      >
      > is a function declaration
      >
      > std::ostringstr eam ss( void );
      >
      > change it to
      >
      > std::ostringstr eam ss;
      >
      > Rob.
      > --
      > http://www.victim-prime.dsl.pipex.com/[/color]


      Comment

      • Rob Williscroft

        #4
        Re: [C++] strange problem with std::ostringstr eam

        Eric Boutin wrote in news:eWjzb.1797 0$xP3.571876@wa gner.videotron. net:
        [color=blue]
        > This is a normal behavior or a behavior from a crappy compiler ?
        >[/color]

        Its normal standard conforming behaviour.

        [snip]. <<-- hint

        Rob.
        --

        Comment

        • Dan W.

          #5
          Re: [C++] strange problem with std::ostringstr eam

          On 03 Dec 2003 11:54:53 GMT, Rob Williscroft
          <rtw@freenet.RE MOVE.co.uk> wrote:
          [color=blue]
          >Eric Boutin wrote in news:eWjzb.1797 0$xP3.571876@wa gner.videotron. net:
          >[color=green]
          >> This is a normal behavior or a behavior from a crappy compiler ?
          >>[/color]
          >
          >Its normal standard conforming behaviour.
          >
          >[snip]. <<-- hint
          >
          >Rob.[/color]

          I've always been a bit confused by declarations of variables
          explicitly calling the default constructor. And now I wonder how would
          the compiler not interpret

          std::ostringstr eam ss();

          as a forward declaration of a function returning an ostringstream.
          When exactly is it necessary to add "()" to class instantiation?

          Would that only be when instantiating a derived type in the argument
          list of a function call like

          class bar {};
          void foo(bar);
          class dbar : bar {};
          ...........
          foo( dbar temp_db() );
          ?

          dan

          Comment

          • Rob Williscroft

            #6
            Re: [C++] strange problem with std::ostringstr eam

            Dan W. wrote in news:8jqrsvcckf qbdj2jgbc08c7e3 tu5ebg593@4ax.c om:
            [color=blue]
            > On 03 Dec 2003 11:54:53 GMT, Rob Williscroft
            > <rtw@freenet.RE MOVE.co.uk> wrote:
            >[color=green]
            >>Eric Boutin wrote in news:eWjzb.1797 0$xP3.571876@wa gner.videotron. net:
            >>[/color][/color]
            [color=blue]
            > I've always been a bit confused by declarations of variables
            > explicitly calling the default constructor. And now I wonder how would
            > the compiler not interpret
            >
            > std::ostringstr eam ss();
            >
            > as a forward declaration of a function returning an ostringstream.
            > When exactly is it necessary to add "()" to class instantiation?[/color]

            Well never (almost). You could write:

            std::ostringstr eam ss = std::ostringstr eam();

            Unfortunatly it won't work in this case as the streams lack copy-ctor's
            but *fortunatly* it unnesassery as the compiler will default construct
            ss for you anyway.
            [color=blue]
            >
            > Would that only be when instantiating a derived type in the argument
            > list of a function call like
            >[/color]

            The one and only time it's truly usefull to explicitly call a default
            constructor is when initialising a POD (int, double, struct without
            constructors etc).

            struct X
            {
            int a; /* a is POD no default constuctor */
            X() : a() {}
            };

            Though a( 0 ) would be just as good above. This is truly usfull within
            templates.

            template < typename T > struct Y
            {
            T b;
            Y() : b() {}
            };

            Now Y::b is properly initialized even if T is a type (say int)
            without a default constructor. Also unless for some reason we
            know that T will always be initializable from 0, b( 0 ) won't
            work.
            [color=blue]
            > class bar {};
            > void foo(bar);
            > class dbar : bar {};
            > ..........
            > foo( dbar temp_db() );[/color]

            Did you mean

            foo( bar() );

            or maybe

            foo( dbar() ); // watch the slicing !!


            struct Z { /*whatever*/ };

            int foo( Z const &z = Z() )
            {
            return 0;
            }

            Not from all of the above the only place I write variable_name() is
            in the initialization list of stucts X and Y, otherwise its type().


            Rob.
            --

            Comment

            • Dan W.

              #7
              Re: [C++] strange problem with std::ostringstr eam

              Thank you!

              I guess I had foggy memories coming up, like in nameless temporary
              construction, such as

              class foggy_memory {};
              ....
              throw( foggy_memory() );

              Such off sightings often throw me off for a sec... ;-)

              Comment

              Working...