stringstream, istream, conversions

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

    #1

    stringstream, istream, conversions

    Hello,

    I have problems understanding why the following example does not compile in
    VC7.1:

    std::stringstre am test;
    const std::istream& test2 = test; //OK
    const std::istream& test3 = std::stringstre am(); // fails

    Error is:
    'std::basic_str ingstream<_Elem ,_Traits,_Alloc >::__ctor' : no non-explicit
    constructor available for implicit conversion

    I used to think test + test2 lines and test3 lines are equivalent but it
    seems not. Any ideas?
    Note: I know that test3 line will lead to undefined behaviour, but I would
    like to know why it does not compile?

    Thanks in advance,

    SerGioGioGio


  • morgan

    #2
    Problem with returning array from function.

    #include<conio. h>
    #include<stdio. h>
    #include<iostre am.h>
    #define re 1 //realis
    #define im 2 //imaginalis

    typedef int complex;

    complex F1 (complex x[2],complex y[2]){
    cout<<"Dodawani e liczb zespolonych"<<
    "("<<x[re]<<"+"<<x[im]<<"i)+"<<"("< <y[re]<<"+"<<y[im]<<"i)=";
    complex z[2];
    z[re]=x[re]+y[re];
    z[im]=x[im]+y[re];
    return z;} // <========Here



    void main()
    {clrscr();
    complex x[2],y[2];
    cout<<"Podaj liczbe rzeczywista do pierwszej liczby zespolnoej x+yi x= ";
    cin>>x[re];
    cout<<"Podaj liczbe urojon¡ do pierwszej liczby zespolnoej x+yi y= ";
    cin>>x[im];
    cout<<"Podaj liczbe rzeczywista do drugiej liczby zespolnoej x+yi x= ";
    cin>>y[re];
    cout<<"Podaj liczbe urojon¡ do drugiej liczby zespolnoej x+yi y= ";
    cin>>y[im];
    complex z[2];
    z=F1(x,y); // <========And here


    cout<<"("<<z[re]<<"+"<<z[im]<<"i)"<<endl;
    getchar();
    }


    Comment

    • Karl Heinz Buchegger

      #3
      Re: Problem with returning array from function.

      morgan wrote:[color=blue]
      >[/color]

      The simple answer is:
      you can't return an array. Just as you cannot assign
      an array:

      int i[5], j[5];

      ...
      i = j; // won't work. You cannot assign arrays.


      In your case a simple solution would be to *not*
      use an array, but use a struct instead:

      struct complex
      {
      int re;
      int im;
      };

      complex F1 (complex x, complex y )
      {
      cout << "Dodawanie liczb zespolonych" <<
      "(" << x.re << "+" << x.im << "i)+" <<
      "(" << y.re << "+" << y.im << "i)=";

      complex z;

      z.re = x.re + y.re;
      z.im = x.im + y.re;
      return z;
      }
      [color=blue]
      > void main()[/color]
      int main()
      {
      complex x, y;
      cout << "Podaj liczbe rzeczywista do pierwszej liczby zespolnoej x+yi x= ";
      cin >> x.re;
      cout << "Podaj liczbe urojon¡ do pierwszej liczby zespolnoej x+yi y= ";
      cin >> x.im;
      cout << "Podaj liczbe rzeczywista do drugiej liczby zespolnoej x+yi x= ";
      cin >> y.re;
      cout << "Podaj liczbe urojon¡ do drugiej liczby zespolnoej x+yi y= ";
      cin >> y.im;

      complex z;
      z = F1( x, y );

      cout << "(" << z.re << "+" << z.im << "i)" << endl;
      getchar();
      }


      --
      Karl Heinz Buchegger
      kbuchegg@gascad .at

      Comment

      • Tom Widmer

        #4
        Re: stringstream, istream, conversions

        On Tue, 14 Dec 2004 08:35:55 +0100, "SerGioGio" <sergiogio@yaho o.fr>
        wrote:
        [color=blue]
        >Hello,
        >
        >I have problems understanding why the following example does not compile in
        >VC7.1:
        >
        > std::stringstre am test;
        > const std::istream& test2 = test; //OK
        > const std::istream& test3 = std::stringstre am(); // fails
        >
        >Error is:
        >'std::basic_st ringstream<_Ele m,_Traits,_Allo c>::__ctor' : no non-explicit
        >constructor available for implicit conversion
        >
        >I used to think test + test2 lines and test3 lines are equivalent but it
        >seems not. Any ideas?
        >Note: I know that test3 line will lead to undefined behaviour, but I would
        >like to know why it does not compile?[/color]

        You cannot bind a temporary to a non-const reference. This is to
        prevent code like this compiling:

        void f(int& i)
        {
        i = 10;
        }

        int main()
        {
        double d = 0;
        f(d); //temporary int created and modified
        //here d is still 0!
        }

        It is legal to bind a temporary to a const reference, since that can't
        have the problem above.

        It has been suggested that the rule would be better if it were: "You
        can't bind a temporary to a non-const reference *unless binding can be
        done without any implicit conversions*." Or something like that.

        Tom

        Comment

        • Mike Wahler

          #5
          Re: stringstream, istream, conversions


          "SerGioGio" <sergiogio@yaho o.fr> wrote in message
          news:41be9617$1 @news.starhub.n et.sg...[color=blue]
          > Hello,
          >
          > I have problems understanding why the following example does not compile[/color]
          in[color=blue]
          > VC7.1:
          >
          > std::stringstre am test;
          > const std::istream& test2 = test; //OK[/color]

          Why 'const'. You won't be able the modify the stream
          (i.e. use it to extract characters).
          [color=blue]
          > const std::istream& test3 = std::stringstre am(); // fails
          >
          > Error is:
          > 'std::basic_str ingstream<_Elem ,_Traits,_Alloc >::__ctor' : no non-explicit
          > constructor available for implicit conversion[/color]

          This answers your last question below.
          [color=blue]
          >
          > I used to think test + test2 lines and test3 lines are equivalent but it
          > seems not. Any ideas?
          > Note: I know that test3 line will lead to undefined behaviour, but I would
          > like to know why it does not compile?
          >
          > Thanks in advance,[/color]

          Also note that streams are not copyable. (Well you *can* copy them,
          but it's a convoluted process (See e.g. Josuttis), and not recommended.

          What specifically are you trying to do?

          -Mike


          Comment

          Working...