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... 3 1905
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/
It compiles on SGI's CC compiler in SGI's IRIX OS.
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/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Nitin Bhardwaj |
last post by:
Thanx in advance for the response...
I wanna enquire ( as it is asked many a times in Interviews that i
face as an Engg PostGraduate ) about the overloading capability of the
C++ Language.
...
|
by: Matthew Polder |
last post by:
Hi,
When a class Apple is written and the assignment operator is not
explicitly declared, the operator
Apple& operator=(const Apple&)
is created by the compiler. Is there any difference...
|
by: August1 |
last post by:
I've written an interface and implementation file along with a client
source file that allows the use of an overloaded subtraction operator.
However, when
using the program, I'm running into a...
|
by: Tony Johansson |
last post by:
Hello Experts!!
I have two small classes called Intvektor and Matris shown at the bottom and
a main.
Class Intvektor will create a one dimension array of integer by allocate
memory dynamically...
|
by: B. Williams |
last post by:
I have an assignment for school to Overload the operators << and >and I
have written the code, but I have a problem with the insertion string
function. I can't get it to recognize the second of...
|
by: raylopez99 |
last post by:
I need an example of a managed overloaded assignment operator for a
reference class, so I can equate two classes A1 and A2, say called
ARefClass, in this manner: A1=A2;. For some strange reason...
|
by: sam_cit |
last post by:
Hi Everyone,
I was just wondering, about the overloaded assignment operator for
user defined objects. It is used to make sure that the following works
properly,
obj1 = obj;
so the...
|
by: subramanian100in |
last post by:
overloaded operator=()
--------------------------------
overloaded assignment operator should be a non-static MEMBER function
of a class. This ensures that the first operand is an lvalue.
If...
|
by: jr.freester |
last post by:
I have created to classes Matrix and System. System is made up of
type matrix.
----------------------------------------------------------------------------------
class Matrix
{
private:
int...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |