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

J

jerry.teshirogi

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...
 
I

Ivan Novick

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/
 
I

Ivan Novick

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/
 
J

James Kanze

On Jun 25, 6:11 pm, (e-mail address removed) 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) };
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top