Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

invalid initialization of non-const reference of type

963 views
Skip to first unread message

Heitber Andres Montilla Ramirez

unread,
May 9, 2020, 8:36:20 PM5/9/20
to
hi everyone I´m learning c++ with a serie of tutorials and I have this error "invalid initialization of non-const reference of type 'Vector3&' from a rvalue of type 'Vector3'

I'd like to know why and how I may solve this issue.

here´s the complete code (the first is the "Vector3.h" file en the other the "main.cpp" file.

#ifndef VECTOR3_H
#define VECTOR3_H

#include <math.h>

class Vector3
{
public:
Vector3(float X = 0.0f, float Y = 0.0f, float Z = 0.0f)
{
x = X;
y = Y;
z = Z;
}


Vector3 operator+=(const Vector3 &vec)
{
return ( *this = (*this + vec) );
}

Vector3 operator+(const Vector3 &vec)
{
return Vector3(vec.x + x, vec.y + y, vec.z +z);
}
//-----------------------------------------------------------
Vector3 operator-=(const Vector3 &vec)
{
return ( *this = (*this - vec) );
}

Vector3 operator-(const Vector3 &vec)
{
return Vector3(x - vec.x, y - vec.y, z - vec.z);
}
//-------------------------------------------------------------
Vector3 operator*=(float num)
{
return ( *this = (*this * num) );
}

Vector3 operator*(float num)
{
return Vector3(x * num, y * num, z * num);
}

//----------------------------------------------------------
Vector3 operator/=(float num)
{
return ( *this = (*this / num) );
}

Vector3 operator/(float num)
{
return Vector3(x / num, y / num, z / num);
}
//---------------------------------------------------------
Vector3 operator-(void)
{
return Vector3(-x, -y, -z);
}

float Dot(Vector3 &vec)
{
return (x * vec.x + y * vec.y + z * vec.z);
}

Vector3 operator*(const Vector3 &vec)
{
return Vector3 (y * vec.z - z * vec.y,
z * vec.x - x * vec.z,
x * vec.y - y * vec.x );
}

float Lenght(void)
{
return sqrt(x * x + y * y + z * z);
}

Vector3 Normalize(void)
{
float lenght = Lenght();

x /= lenght;
y /= lenght;
z /= lenght;

return *this;
}

float Distance(Vector3 &vec)
{
float disX = vec.x - x;
float disY = vec.y - y;
float disZ = vec.z -z;

return sqrt(disX * disX + disY * disY + disZ * disZ);
}

bool operator==(Vector3 &vec)
{
return (vec.x == x && vec.y == y && vec.z == z);
}

bool operator!=(Vector3 &vec)
{
return !(vec == *this);
}

public:
float x, y, z;

};

#endif // VECTOR3_H


-------------------------------------------------------------------------

#include <iostream>
#include "Vector3.h"

using namespace std;

void printVector(Vector3 &vec)
{
cout << vec.x << ", " << vec.y << ", " << vec.z << endl;
}

int main()
{
printVector( Vector3(1, 2, 3) + Vector3(7, 2, 5) );
printVector( Vector3(3, 4, 5) - Vector3(2, 9, 3) );

printVector( Vector3(1, 2, 3) * 5);

cout << Vector3(1, 2, 3).Dot( Vector3(3, 2, 1) ) << endl;

printVector(Vector3(1, 2, 3) * Vector3(4, 5, 6) );

}

Ian Collins

unread,
May 9, 2020, 8:53:11 PM5/9/20
to

On 10/05/2020 12:36, Heitber Andres Montilla Ramirez wrote:
> hi everyone I´m learning c++ with a serie of tutorials and I have
> this error "invalid initialization of non-const reference of type
> 'Vector3&' from a rvalue of type 'Vector3'
>
> I'd like to know why and how I may solve this issue.
>
> here´s the complete code (the first is the "Vector3.h" file en the
> other the "main.cpp" file.

Showing where the error occurs would be helpful, but I'm guessing it's
this line

printVector( Vector3(1, 2, 3) * 5);

If so, the reason is you can't initialise a non-const reference from a
temporary object. You need to update printVector to take a const
reference parameter:

void printVector( const Vector3 &vec)

<snip code>

--
Ian.

Jorgen Grahn

unread,
May 10, 2020, 3:19:22 AM5/10/20
to
And that's what you want anyway, since you surely don't intend to let
printVector() modify the vector it prints.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Bo Persson

unread,
May 10, 2020, 6:40:57 AM5/10/20
to
And additionally - the origin of this rule (no non-const reference to
temporaries) comes from the fact that any modifications will be lost
anyway when the temporary immediately goes away.

Apparently Bjarne and his colleagues had some initial problem with this,
before he invented that rule.


Bo Persson

Heitber Andres Montilla Ramirez

unread,
May 10, 2020, 3:11:28 PM5/10/20
to
thanks man!!
0 new messages