use of class template requires template argument list

Started by
6 comments, last by ExcessNeo 16 years, 4 months ago
Hi people, Recently I bought Ron Penton's Data Structure's book (unfortonutally it has no cd, if anybody has a link, I appreciate .. ) and began reading the first chapter which introduces the arrays. I try to test the aray class he was explaining in the book but my compiler keep telling me that my class template requires the argument list ? which according to my very limited knowledge, includes. Would somebody please tell me what I was doing wrong? BTW, I am using Visual Studio 2005 and trying to compile this code as a console application. Thanks all. Array.h

#pragma once

template <class DataType> 

class Array
{
public:
	Array(int p_size);
	DataType* m_array;
	int m_size;
public:
	~Array(void);
};
Array.cpp

#include "StdAfx.h"
#include "Array.h"

Array::Array(int p_size)
{
	m_array = new DataType[p_size];
	m_size = p_size;
}

Array::~Array(void)
{
	if (m_array !=0) delete[] m_array;
	m_array=0;
}
main.cpp

#include "stdafx.h"
#include "Array.h"


int _tmain(int argc, _TCHAR* argv[])
{
        Array <int> intArray(10);
	

	return 0;
}

Advertisement
AFAIK, the template argument list must also be included in the function definition, e.g.:
template <class DataType>Array<DataType>::Array(int p_size){    m_array = new DataType[p_size];    m_size = p_size;}
There are a couple of other funky things about that code that I'll mention:

1. Public member data (which doesn't seem like it ought to be public)
2. No use of initializer lists
3. Unnecessary check for null in the destructor
4. Unnecessary assignment of null in the destructor
When coding templated classes, the whole template definition (including the implementation of member functions) must be in the header file, so forget .cpp files. This is needed as the code from a template must be available to every module which uses it.

EDIT :
// Array.h#pragma once // Microsoft-specifictemplate <class DataType> class Array{public:	// You might prefer "unsigned int" or better, "size_t" to your "int"	Array(int p_size)	{		// What if p_size <= 0?		m_array = new DataType[p_size];		m_size = p_size;	}	// Information hiding anyone?	// What if I decide to modify those?	DataType* m_array;	int m_size;public:	~Array(void)	{		// Why do this test? your constructor ALWAYS allocates memory.		if (m_array !=0) delete[] m_array;		// Unnecessary, the object is to be obliterated from your ram in a nanosecond		m_array=0;	}	// Copy constructor anyone?	// Assignment operator overloading anyone?};


Sorry for the little comments, I couldn't stop me. And remember that the stl has an implementation of what you're doing called "std::vector" which can only be superior to yours =). But of course if it's only for learning purposes...

[Edited by - Trillian on November 19, 2007 8:38:51 PM]
Thank you guys : ) I understand it now.

One question though, isn't it always a good practice to assign a pointer
to null after we are finished with it whether it is necessary or not ?
Quote:Original post by jyk
AFAIK, the template argument list must also be included in the function definition, e.g.:
template <class DataType>Array<DataType>::Array(int p_size){    m_array = new DataType[p_size];    m_size = p_size;}


That is correct.
Quote:Original post by jyk
AFAIK, the template argument list must also be included in the function definition, e.g.:
template <class DataType>Array<DataType>::Array(int p_size){    m_array = new DataType[p_size];    m_size = p_size;}
There are a couple of other funky things about that code that I'll mention:

1. Public member data (which doesn't seem like it ought to be public)
2. No use of initializer lists
3. Unnecessary check for null in the destructor
4. Unnecessary assignment of null in the destructor



MSDN:

If unsuccessful, new returns zero or throws an exception; see The new and delete Operators for more information. You can change this default behavior by writing a custom exception-handling routine and calling the _set_new_handler run-time library function with your function name as its argument.

What if the new call is unsuccessfull ? Isn't that makes the use of null check at the destructor necessary ?
No, calling delete on a null pointer is allowed.
Quote:Original post by _ArmuT_
What if the new call is unsuccessfull ? Isn't that makes the use of null check at the destructor necessary ?


You should be doing the null checking in your constructor if anywhere.

This topic is closed to new replies.

Advertisement