Very simple modification of Methods

{Apologies for any incorrect language and syntax when describing this, i'm a complete novice at c++!} I want to modify a method which i've defined as a vector, with another method so I can easily modify the vector components but i'm having some trouble. This is my vector method:

1
2
3
4
5
6
7
8
9
// Cartesian constructor
    threevector(double x, double y, double z)
    {
        xcoord = x;
        ycoord = y;
        zcoord = z;
        
        cartesian = true;
    }


I have already defined xcoord, yco...etc in the private section of my class, and the cartesian = true is just there because I used spherical polar later and wanted to differentiate in other methods.
In my main I want to be able to modify some generic threevector (call it v1, with coordinates (1, 2, 3)) with a new method, setx(double ...). Here is my current code for this modifier:

1
2
3
4
5
// Modifier method for cartesian coordinates
    void setx(double x)
    {
        x = xcoord;
    }


I'm worried about this code fragment writing the current xcoord value (xcoord of v1 = 1) in the memory location that x (x = 2.0 in the case below) is stored and not the other way around, where the latter is the clearly desired result. Furthermore, an error which I dont understand was returned when I tried to call this method in my main with the following code:

 
cout << v1.setx(2.0) << endl;


The error returned was as follows:
"Invalid operands to binary expression ('ostream'(aka 'basic_ostream<char>') and 'void')

Any ideas how to fix this?
Thanks, any help appreciated!
Last edited on
1
2
3
4
5
// Modifier method for cartesian coordinates
    void setx(double x)
    {
        xcoord  = x ;
    }


Does that work?

Did you follow my earlier advice about naming member variables with a leading m_ ?

If you have question about the same code you had in an earlier topic, then keep that topic going, don't start a new one.

HTH
Sorry! I didn't realise about the same topic thing, and yeah I've made all the new variables with the m_ precursor, I didn't change my old ones because its an assignment by my uni to and that new method i'm trying is using the old variables.

I'll give that a try btw, thanks!
I tried the method you outlined above but when calling the function again in my main it returned the same error. I think it's messing up when i call the function, but I have no idea how to mend it
@Gunnir, it should be just

v1.setx(2.0);

instead of

cout << v1.setx(2.0) << endl; // invalid

since you are not printing anything to the user.
ahhhhhh of course i'm not, its a void function, thanks!
Topic archived. No new replies allowed.