"Expression must be a pointer to complete object type" error with templated class?

Started by
1 comment, last by johnmarinelli 10 years, 8 months ago

Hello everyone,

I'm working on a little exercise program that takes a matrix and a vector and performs algebra on them.

I'm using templates for both my 4x4 matrix class and my vector4 class.

My matrix4x4 class serves as a parent to specific types of matrices, like a translation matrix.

The program gives me an error when I instantiate a matrix4x4 class with no parameters and try to operate on it, even though I fill the values of the object in an empty constructor:


vector4<T> translateVector(const matrix4x4<T> matrix, const vector4<T> vector)
{
	matrix4x4<T> tempMatrix();

	for (int row = 0; row < MAX_LENGTH; ++row)
	{
		for (int column = 0; column < MAX_LENGTH; ++column)
		{
			tempMatrix[row][column] = (matrix[row][column] * vector[row]);  //I get errors on this line and the one below
			newVector[row] += tempMatrix[row][column];
		}
	}
}


//in matrix4x4.h:

template <typename T>
class matrix4x4
{
public:
	T values[MAX_LENGTH][MAX_LENGTH];

matrix4x4() //when using this ctor, the compiler tells me I need to have a pointer to complete object type
	{
		for (int i = 0; i < MAX_LENGTH; i++)
		{
			for (int j = 0; j < MAX_LENGTH; j++)
			{
				values[i][j] = 0;
			}
		}
	};

T* operator[](int index)
    {
        assert (index > -1 && index < MAX_LENGTH);
        return values[index];
    };

However, I fixed the problem by simply adding this ctor to the matrix4x4 class:


matrix4x4(T x) //this does exactly the same thing as empty ctor, just has a parameter
	{
		for (int i = 0; i < MAX_LENGTH; i++)
		{
			for (int j = 0; j < MAX_LENGTH; j++)
			{
				values[i][j] = 0;
			}
		}
	};

I feel like this is a bad patch to a larger problem I should address, but I'm not sure what that problem is or how to fix it.

Is it just a part of C++ to consider a templated object with an empty ctor to be "incomplete"?

Advertisement
This line doesn't do what you probably think it does:

matrix4x4<T> tempMatrix();
You probably think it creates a matrix named tempMatrix. It actually creates a function declaration for a function taking no arguments and returning a matrix. If you want to create an object that is default constructed you need to leave off the parentheses.

Of course it's a simple answer! dry.png

Thanks very much!

This topic is closed to new replies.

Advertisement