Hi,

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

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

fourMatrix.h

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

...
}

fourMatrix.cpp:

fourMatrix::fourMatrix():i(fourVector(1,0,0,0)),j(fourVector(0,1,0,0)),k(fourVector(0,0,1,0)),l(fourVector(0,0,0,1)){}

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;
	n.i=fourVector(1,0,0,0);
	n.j=fourVector(0,cos(a),sin(a),0);
	n.k=fourVector(0,-sin(a),cos(a),0);
	n.l=fourVector(0,0,0,1);

	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),
fourVector(0,-sin(a),cos(a),0),fourVector(0,0,0,1);

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

Recommended Answers

All 2 Replies

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{
public:
	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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.