Jump to content

  • Log In with Google      Sign In   
  • Create Account


use of class template requires template argument list


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
7 replies to this topic

#1 _ArmuT_   Members   -  Reputation: 122

Like
0Likes
Like

Posted 19 November 2007 - 02:01 PM

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;
}



Sponsor:

#2 scgames   Members   -  Reputation: 1969

Like
0Likes
Like

Posted 19 November 2007 - 02:10 PM

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

#3 Trillian   Members   -  Reputation: 410

Like
0Likes
Like

Posted 19 November 2007 - 02:38 PM

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-specific

template <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]

#4 _ArmuT_   Members   -  Reputation: 122

Like
0Likes
Like

Posted 19 November 2007 - 03:23 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 ?


#5 ExcessNeo   GDNet+   -  Reputation: 416

Like
0Likes
Like

Posted 19 November 2007 - 03:23 PM

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.

#6 _ArmuT_   Members   -  Reputation: 122

Like
0Likes
Like

Posted 19 November 2007 - 03:32 PM

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 ?


#7 incin   Members   -  Reputation: 205

Like
0Likes
Like

Posted 19 November 2007 - 03:44 PM

No, calling delete on a null pointer is allowed.

#8 ExcessNeo   GDNet+   -  Reputation: 416

Like
0Likes
Like

Posted 19 November 2007 - 04:44 PM

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.




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