By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,900 Members | 1,078 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,900 IT Pros & Developers. It's quick & easy.

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

P: n/a
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[i] << 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 27 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Jun 25, 6:11*pm, jerry.teshir...@gmail.com 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[i] << 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/
Jun 27 '08 #2

P: n/a
It compiles on SGI's CC compiler in SGI's IRIX OS.

Jun 27 '08 #3

P: n/a
On Jun 25, 6:11*pm, jerry.teshir...@gmail.com 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[i] << 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/
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.