Archived

This topic is now archived and is closed to further replies.

initialization of non-const reference type

This topic is 5618 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! What does this error mean? I cannot find out. test.cpp: In function `void move()'': test.cpp:207: initialization of non-const reference type `struct vec3 &'' test.cpp:207: from rvalue of type `vec3'' ../gamelibrary/misc/../../matphylib/rigid_body.h:38: in passing argument 1 of `RigidBody::AddForce(vec3 &, vec3 &)'' Thanks

Share this post


Link to post
Share on other sites
IIRC you can only pass temporary objects to const references. Try defining the objects you want to pass to the function before you call the function.


  
void function (vector& v);

int main ();
{
// This shouldn't work

function (vector (1.0, 1.0, 1.0));

// This should work fine

vector v (1.0, 1.0, 1.0);
function (v);
}



[edited by - null_pointer on July 26, 2002 1:54:05 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by SabreMan
Show us the code!


Here it comes...


  

// Here''s the error

ship_body.AddForce( vec3(0,10 ,0), ship_body.m_Position );
// it''s in the first parameter


// Here''s the AddForce method

class RigidBody
{
public:

void AddForce(vec3 &Force, vec3 &Position);
};

// Here''s the vec3 class

struct vec3
{
float x, y, z;

vec3() {}
vec3(float _x,float _y,float _z) { x=_x; y=_y; z=_z; }

void set(float _x,float _y,float _z) { x=_x; y=_y; z=_z; }

vec3 operator- () const;

vec3& operator/= (float d);
vec3& operator*= (float m);
vec3& operator+= (const vec3& v);
vec3& operator-= (const vec3& v);

vec3 operator+ (const vec3& v) const;
vec3 operator- (const vec3& v) const;
vec3 operator* (float m) const;
vec3 operator/ (float d) const;
friend vec3 operator* (float m, const vec3& v);

bool operator== (const vec3& v) const;
bool operator!= (const vec3& v) const;

};


Share this post


Link to post
Share on other sites
null_pointer, that''s exactly what''s the problem.

// This shouldn''t work
function (vector (1.0, 1.0, 1.0));

And it''s the problem.

// This should work fine
vector v (1.0, 1.0, 1.0);
function (v);

Yes, this works me too.
But I''d like the previous to work.
But if I can''t, then no problem using the latter

thaks.

Share this post


Link to post
Share on other sites
Make the force a const reference, then it should work fine:
void AddForce(const vec3 &Force, vec3 &Position);

It''s not correct unless it''s const-correct.

Share this post


Link to post
Share on other sites