List of doubles for overloaded assignment operator's RVALUE???

Discussion in 'C++' started by jerry.teshirogi@gmail.com, Jun 26, 2008.

  1. Guest

    I have the following class and main:

    //////////////////////////////////////////////////////////
    #include <iostream.h>

    class myVector
    {
    public:
    double x, y, z:

    void set( const double &, const double &, const double &);
    friend ostream & operator<<( ostream &, const vector & );
    }

    ////////////////////////////////////////////////////////////

    void myVector::set( const double &xin, const double &yin, const double
    &zin )
    {
    x = xin;
    y = yin;
    z = zin;
    }

    ostream & operator<<( ostream &os, const vector &v )
    {
    os << v.x << '\t' << v.y<< '\t' << v.z;
    return os;
    }

    //////////////////////////////////////////////////////////

    1:int main()
    2:{
    3: double dra[3] = {0.0, 0.1, 0.2};
    4:
    5: myVector v0 = {0.0, 0.2, 0.4};
    6:
    7: myVector v1[2] = { {0.0, 1.0, .2.0}, {3.0, 4.0, 5.0} };

    8: cout << "double dra[3] = " << dra[0] << '\t' << dra[1] << '\t' <<
    dra[2] << endl;
    9: cout << "v0 = " << v0 << endl;
    10: for( int i; i < 2; i ++ )
    11: cout << "v1[" << i << "] = " << v1 << endl;
    12:}

    The above code illustrates a portion of a large legacy simulation that
    I'm looking to upgrade.

    I would like to add much more functionality to myVector class. To
    start off, I would like to add constructors, and make the member
    variables x, y, & z private scope. But I found that if I do either
    one of those things, the compiler squawks on lines 5 & 7 in the main,
    and says that myVector class doesn't provide suitable functions to
    implement those lines. And if I take out the constructors and set my
    member variables back to public scope, the compiler stops complaining.

    It's as if my adding stuff to the class is taking away an inherent
    hidden functionality of the class. But I need to keep that
    functionality, because the legacy simulation demands it; the sim
    assigns values to myVector instances from lists of doubles, like this,

    myVector v0 = {0.0, 0.2, 0.4};

    My solution approach so far has been to add an overloaded assignment
    operator (operator=) to work for lines 5 & 7. The problem I'm having
    is, I don't know how to declare the RVALUE portion (i.e., {0.0, 0.2,
    0.4} ) in the declaration of the overloaded operator=.

    vector & operator=( ???????? ) ;

    I'm not entirely sure if it's actually possible to declare RVALUES
    like this. But I'm betting it's doable. Array of doubles, array
    of floats, and array of ints can be initialized like in line # 3 in
    the main. And if those guys are all C++ classes and have that
    functionality, then there ought to be a way to add the same capability
    in to my class, right?

    But I'm stuck...

    I'm open to suggestions on other approaches as well.

    My desire is really to be able to add constructors, private variables,
    and a host of other functions to the class, and, of course, also be
    able to assign values to myVector instances from a list as in lines 5
    & 7.

    Thanks in advance for your help...
    , Jun 26, 2008
    #1
    1. Advertising

  2. Ivan Novick Guest

    On Jun 25, 6:11 pm, wrote:
    > I have the following class and main:
    >
    > //////////////////////////////////////////////////////////
    > #include <iostream.h>
    >
    > class myVector
    > {
    >    public:
    >       double x, y, z:
    >
    >       void set( const double &, const double &, const double &);
    >       friend ostream & operator<<( ostream &, const vector & );
    >
    > }
    >
    > ////////////////////////////////////////////////////////////
    >
    > void myVector::set( const double &xin, const double &yin, const double
    > &zin )
    > {
    >    x = xin;
    >    y = yin;
    >    z = zin;
    >
    > }
    >
    > ostream & operator<<( ostream &os, const vector &v )
    > {
    >    os << v.x << '\t' << v.y<< '\t' << v.z;
    >    return os;
    >
    > }
    >
    > //////////////////////////////////////////////////////////
    >
    > 1:int main()
    > 2:{
    > 3:   double dra[3] = {0.0, 0.1, 0.2};
    > 4:
    > 5:   myVector v0 = {0.0, 0.2, 0.4};
    > 6:
    > 7:   myVector v1[2] = { {0.0, 1.0, .2.0}, {3.0, 4.0, 5.0} };
    >
    > 8:   cout << "double dra[3] = " << dra[0] << '\t' << dra[1] << '\t' <<
    > dra[2] << endl;
    > 9:   cout << "v0 = " << v0 << endl;
    > 10:  for( int i; i < 2; i ++ )
    > 11:     cout << "v1[" << i << "] = " << v1 << endl;
    > 12:}
    >


    Hmmm... your code looks to have many syntax issues as posted. For
    example, I don't see how the initialization on line 5 of main can be
    legal. What compiler did you use to compile the posted code?

    Ivan Novick
    http://www.mycppquiz.com/
    Ivan Novick, Jun 26, 2008
    #2
    1. Advertising

  3. Guest

    It compiles on SGI's CC compiler in SGI's IRIX OS.
    , Jun 26, 2008
    #3
  4. Ivan Novick Guest

    On Jun 25, 6:11 pm, wrote:
    > I have the following class and main:
    >
    > //////////////////////////////////////////////////////////
    > #include <iostream.h>
    >
    > class myVector
    > {
    >    public:
    >       double x, y, z:
    >
    >       void set( const double &, const double &, const double &);
    >       friend ostream & operator<<( ostream &, const vector & );
    >
    > }
    >
    > ////////////////////////////////////////////////////////////
    >
    > void myVector::set( const double &xin, const double &yin, const double
    > &zin )
    > {
    >    x = xin;
    >    y = yin;
    >    z = zin;
    >
    > }
    >
    > ostream & operator<<( ostream &os, const vector &v )
    > {
    >    os << v.x << '\t' << v.y<< '\t' << v.z;
    >    return os;
    >
    > }
    >
    > //////////////////////////////////////////////////////////
    >
    > 1:int main()
    > 2:{
    > 3:   double dra[3] = {0.0, 0.1, 0.2};
    > 4:
    > 5:   myVector v0 = {0.0, 0.2, 0.4};
    > 6:
    > 7:   myVector v1[2] = { {0.0, 1.0, .2.0}, {3.0, 4.0, 5.0} };
    >
    > 8:   cout << "double dra[3] = " << dra[0] << '\t' << dra[1] << '\t' <<
    > dra[2] << endl;
    > 9:   cout << "v0 = " << v0 << endl;
    > 10:  for( int i; i < 2; i ++ )
    > 11:     cout << "v1[" << i << "] = " << v1 << endl;
    > 12:}
    >
    > The above code illustrates a portion of a large legacy simulation that
    > I'm looking to upgrade.
    >
    > I would like to add much more functionality to myVector class.   To
    > start off, I would like to add constructors, and make the member
    > variables x, y, & z private scope.  But I found that if I do either
    > one of those things, the compiler squawks on lines 5 & 7 in the main,
    > and says that myVector class doesn't provide suitable functions to
    > implement those lines.   And if I take out the constructors and set my
    > member variables back to public scope, the compiler stops complaining.
    >
    > It's as if my adding stuff to the class is taking away an inherent
    > hidden functionality of the class.   But I need to keep that
    > functionality, because the legacy simulation demands it; the sim
    > assigns values to myVector instances from lists of doubles, like this,
    >
    > myVector v0 = {0.0, 0.2, 0.4};
    >
    > My solution approach so far has been to add an overloaded assignment
    > operator (operator=) to work for lines 5 & 7.   The problem I'm having
    > is, I don't know how to declare the RVALUE portion (i.e., {0.0, 0.2,
    > 0.4} ) in the declaration of the overloaded operator=.
    >
    > vector & operator=( ???????? ) ;
    >
    > I'm not entirely sure if it's actually possible to declare RVALUES
    > like this.    But I'm betting it's doable.   Array of doubles, array
    > of floats, and array of ints can be initialized like in line # 3 in
    > the main.  And if those guys are all C++ classes and have that
    > functionality, then there ought to be a way to add the same capability
    > in to my class, right?
    >
    > But I'm stuck...
    >
    > I'm open to suggestions on other approaches as well.
    >
    > My desire is really to be able to add constructors, private variables,
    > and a host of other functions to the class, and, of course, also be
    > able to assign values to myVector instances from a list as in lines 5
    > & 7.
    >
    > Thanks in advance for your help...


    It seems that your class is "POD struct" for which the compiler allows
    the memberwise initialization that you have provided for the myVector
    class. If you want to add to the class, and hence make it not a POD
    struct, than you will need to change the initialization you use to
    call the constructor and not to use that initializer list that you are
    currently using.

    Hope that helps,
    Ivan Novick
    http://www.mycppquiz.com/
    Ivan Novick, Jun 27, 2008
    #4
  5. James Kanze Guest

    On Jun 26, 3:41 am, Ivan Novick <> wrote:
    > On Jun 25, 6:11 pm, wrote:


    > > I have the following class and main:


    > > //////////////////////////////////////////////////////////
    > > #include <iostream.h>


    > > class myVector
    > > {
    > > public:
    > > double x, y, z:


    > > void set( const double &, const double &, const double &);
    > > friend ostream & operator<<( ostream &, const vector & );
    > > }


    > > ////////////////////////////////////////////////////////////


    > > void myVector::set( const double &xin, const double &yin, const double
    > > &zin )
    > > {
    > > x = xin;
    > > y = yin;
    > > z = zin;
    > > }


    > > ostream & operator<<( ostream &os, const vector &v )
    > > {
    > > os << v.x << '\t' << v.y<< '\t' << v.z;
    > > return os;
    > > }


    > > //////////////////////////////////////////////////////////


    > > 1:int main()
    > > 2:{
    > > 3: double dra[3] = {0.0, 0.1, 0.2};
    > > 4:
    > > 5: myVector v0 = {0.0, 0.2, 0.4};
    > > 6:
    > > 7: myVector v1[2] = { {0.0, 1.0, .2.0}, {3.0, 4.0, 5.0} };

    >
    > > 8: cout << "double dra[3] = " << dra[0] << '\t' << dra[1] << '\t' <<
    > > dra[2] << endl;
    > > 9: cout << "v0 = " << v0 << endl;
    > > 10: for( int i; i < 2; i ++ )
    > > 11: cout << "v1[" << i << "] = " << v1 << endl;
    > > 12:}


    > Hmmm... your code looks to have many syntax issues as posted. For
    > example, I don't see how the initialization on line 5 of main can be
    > legal. What compiler did you use to compile the posted code?


    There are a lot of fairly obvious typos: a colon in place of a
    semi-colon, a missing semi-colon at the end of the class
    definition, and two decimal points in one of the floating point
    numbers, but once those are corrected, there should be no
    problem.

    With regards to the original question: if he doesn't want to
    rewrite the whole code, he's probably stuck with keeping the
    type an agglomerate, which means public data and no
    constructors. If he's very sure that there is nowhere which
    depends on static initialization, however, the rewrite shouldn't
    be too difficult: once you add the constructors, things like:
    myVector v0 = {0.0, 0.2, 0.4};
    myVector v1[2] = { {0.0, 1.0, 2.0}, {3.0, 4.0, 5.0} };
    become
    myVector v0 (0.0, 0.2, 0.4);
    myVector v1[2] = { myVector(0.0, 1.0, 2.0), myVector(3.0, 4.0,
    5.0) };

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Jun 27, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nitin Bhardwaj
    Replies:
    8
    Views:
    843
    Andrey Tarasevich
    Jul 14, 2003
  2. dan
    Replies:
    1
    Views:
    2,299
    Jack Klein
    Nov 26, 2003
  3. August1
    Replies:
    9
    Views:
    498
    Risto Lankinen
    Sep 20, 2004
  4. Kavya
    Replies:
    9
    Views:
    493
    Dik T. Winter
    Oct 28, 2006
  5. Juha Nieminen
    Replies:
    13
    Views:
    596
    Edek Pienkowski
    Aug 29, 2012
Loading...

Share This Page