Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 johnmarinelli   Members   -  Reputation: 304

Like
0Likes
Like

Posted 03 August 2013 - 12:50 PM

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"?


Edited by johnmarinelli, 03 August 2013 - 12:59 PM.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9599

Like
1Likes
Like

Posted 03 August 2013 - 12:55 PM

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.

#3 johnmarinelli   Members   -  Reputation: 304

Like
0Likes
Like

Posted 03 August 2013 - 01:01 PM

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

Thanks very much!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS