I am writing a simple graphics engine using homegeneous coordinates (fourVector) and the corresponding 4matrices (fourMatrix).

fourVector::fourVector(float x, float y, float z, float w): x(x),y(y),z(z),w(w){


class fourMatrix{
	fourVector i,j,k,l;




fourMatrix::fourMatrix(fourVector &i, fourVector &j, fourVector &k, fourVector &l): i(i),j(j),k(k),l(l){

inline fourMatrix& fourMatrix::rotationX(float a){
	fourMatrix n;

	return n;

The above code compiles but seems rather bloated(especially for an inline function): I want to avoid the definition of the local fourMatrix n, so that I write something like:

inline fourMatrix& fourMatrix::rotationX(float a){

	return fourMatrix(fourVector(1,0,0,0),fourVector(0,cos(a),sin(a),0),

However when I try this I get the following error

../Geometry/fourMatrix.cpp: In member function ‘fourMatrix& fourMatrix::rotationX(float)’:
../Geometry/fourMatrix.cpp:351: error: no matching function for call to ‘fourMatrix::fourMatrix(fourVector, fourVector, fourVector, fourVector)’
../Geometry/fourMatrix.cpp:156: note: candidates are: fourMatrix::fourMatrix(float, float, float, float)
../Geometry/fourMatrix.cpp:123: note:                 fourMatrix::fourMatrix(float, float)
../Geometry/fourMatrix.cpp:112: note:                 fourMatrix::fourMatrix(float)
../Geometry/fourMatrix.cpp:23: note:                 fourMatrix::fourMatrix(const Vector&, const Vector&, const Vector&)
../Geometry/fourMatrix.cpp:104: note:                 fourMatrix::fourMatrix(fourVector&)
../Geometry/fourMatrix.cpp:100: note:                 fourMatrix::fourMatrix(Vector&)
../Geometry/fourMatrix.cpp:207: note:                 fourMatrix::fourMatrix(fourMatrix&)
../Geometry/fourMatrix.cpp:14: note:                 fourMatrix::fourMatrix(fourVector&, fourVector&, fourVector&, fourVector&)
../Geometry/fourMatrix.cpp:12: note:                 fourMatrix::fourMatrix()

and if I rewrite the fourMatrix constructor

fourMatrix::fourMatrix(fourVector i, fourVector j, fourVector k, fourVector l): i(i),j(j),k(k),l(l){

I get the following error.

../Geometry/fourMatrix.cpp: In member function ‘fourMatrix& fourMatrix::rotationX(float)’:
../Geometry/fourMatrix.cpp:354: error: invalid initialization of non-const reference of type ‘fourMatrix&’ from a temporary of type ‘fourMatrix’

Clearly I am doing something fundamentally wrong. The problem lies with the fourMatrix constructor, to which I prefer to pass the fourVector objects as a reference.

Thanks in advance for constructive responses

7 Years
Discussion Span
Last Post by MrSpigot

did you try:

fourMatrix::fourMatrix(fourVector a, fourVector b, fourVector c, fourVector d): i(a),j(b),k(c),l(d){

else the compiler takes "i" as local variable

or use:

class fourMatrix{
	fourVector m_i,m_j,m_k,m_l;


then you see is a class attribute

fourMatrix::fourMatrix(fourVector i, fourVector j, fourVector k, fourVector l): m_i(i), m_j(j), m_k(k), m_l(l){

As the compiler tells you, your problem is that the return statement in rotationX is creating a temporary fourMatrix value for which you are trying to return a reference. Returning a reference to a temporary variable is meaningless since it will refer to nothing when the method is complete. Your first version of the method, in which you return a reference to the local variable 'n', would have similar problems although the compiler doesn't pick it up.
You can solve the problem by changing the return signature of the method so that it returns a fourMatrix variable (rather than a fourMatrix&). It's slower, as the temporary you create in the return statement has to be copied, but it works. You'll should also consider a copy constructor in fourMatrix if you don't have one.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.