i can't send struct to a template???

Started by
9 comments, last by dabutabey 22 years, 5 months ago
when i use the func like this:
  
lump_oku<texture>(1);
  
it gives an error like this: "error C2275: 'texture' : illegal use of this type as an expression" struct is like this:
   
struct texture
{
	char name[64];
	int flags;
	int contents;
};
  
the function is like this:
   
template <class lump>
lump* lump_oku(int lump_no)
{
	lump*gecici;
	int Length=bsp.header.direntries[lump_no].length;
	int Offset=bsp.header.direntries[lump_no].offset;
	int Adet=Length/sizeof(lump);
	fseek(dosya,Offset,0);
	gecici=(lump*)malloc(Length);
	for(int i=0;i<Adet;i++)
		fread(&gecici[i],sizeof(lump),1,dosya);
	return &gecici
}    
Edited by - dabutabey on November 3, 2001 5:04:03 AM
Davut Engin
Advertisement
I could be wrong, but I believe MSVC6 doesn'y support that type of template. The template parameter must be used in the argument list of the function.

    template <class lump>void lump_oku(int lump_no, lump*& gecici){	//lump*gecici;	int Length=bsp.header.direntries[lump_no].length;	int Offset=bsp.header.direntries[lump_no].offset;	int Adet=Length/sizeof(lump);	fseek(dosya,Offset,0);	gecici=(lump*)malloc(Length);	for(int i=0;i<Adet;i++)		fread(&gecici[i],sizeof(lump),1,dosya);}    




[edit: change it to a lump*& from lump*, it may actually work now)

Edited by - Magmai Kai Holmlor on November 3, 2001 11:59:55 PM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Try changing this:
  template <class lump>  

to this:
  template <typename lump>  


- Ian Perez (iperez.fett@verizon.net) - "It is by will alone I set my mind in motion"

Edited by - [bobafet] on November 3, 2001 12:56:05 PM
- Ian Perez (iperez.fett@verizon.net) - "It is by will alone I set my mind in motion"
[bobafet]''s method won''t do anything for you, since structs are classes. You can use typename and class interchangeably.

In MSVC, you never instantiate a template function by inserting the type after the function name, ie, uou never do this:
function&lttype_class>(arg_list); 

As Magmai said, you must have at least one parameter in the argument list that is of the instantiation type, from which the compiler will deduce the appropriate template class. Rather unfortunate.

So it''s back to the design board for you...

I''m not sure if you can determine from the object receiving a return value, essentially by declaring your template function to return the typename. To my mind, that would cause a lot of ambiguity. In any case, what are you trying to acheive; there may be a better or simpler way.
quote:
...there may be a better or simpler way.

Yeah, simply pass a pointer reference (or a pointer-pointer) into the template function :p

And I ran into a case where I needed to specify the template arguement - oh wait that for for a class template not a function template. (MI with the same template base class inherited with different template parameters).

I thought I'd be ambiguous as well - but for this particular case you specify the template arguement to the function to avoid that ambiguity. It is part of the C++ spec, and it does work on other (non-M$) compilers. I think I read somewhere that MSVC7(.Net) supports more of the template magic.

Magmai Kai Holmlor
- Not For Rent

Edited by - Magmai Kai Holmlor on November 3, 2001 11:59:26 PM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
I''m able to pass a class as a template parameter in my code (in MSVC 6), maybe I''m doing something different?
template  inline T *rNew(void) {	#ifdef NEW_USES_EXCEPTIONS		T *Get;		try {			Get = new T;		} catch(exception) {			return NULL;		}		return Get;	#else		return new T;	#endif}// Called like this (actual snippet of code too):Vertices = rNew(PerRow*Rows);

That works for me (I use it pretty frequently). rVertex is a class too (actually a struct, same difference).

[Resist Windows XP''s Invasive Production Activation Technology!]
Ok, screw it . If I use source tags my code gets mangled, if I use pre/code tags my template parameters get removed. Damn it. That''s meant to have <class T> after template and <rVertex> after rNew.
quote:Original post by Null and Void
Ok, screw it . If I use source tags my code gets mangled, if I use pre/code tags my template parameters get removed. Damn it. That''s meant to have after template and after rNew.

Use one of & lt and & gt (without the spaces); the board interprets anything between angular brackets as HTML.
std::priority_queue&ltprint_job> print_queue; 




I wanna work for Microsoft!
Oluseyi, it didn''t work for you . Next time I''m writing it out by hand in HTML (&nbsp;''s and &gt;''s, heh) without any of the broken code formating thingies.

[Resist Windows XP''s Invasive Production Activation Technology!]
quote:Original post by Magmai Kai Holmlor
I could be wrong, but I believe MSVC6 doesn''y support that type of template. The template parameter must be used in the argument list of the function.


Not true. VC++ has a bug with explicit specified template only if the template arguments are not used in return value or parameters.

This compiles fine.

  #include <iostream>using	namespace std;struct X{};template <class T>	T	F(){	cout << typeid(T).name() << endl;	return	T();}int	main(){	F<X>();	return	0;}  


The problem with VC++ 6.0 pops up(again) if you try to specialize F(). If not, the function is fine.

To workaround, passing a dummy parameter would work. However, Andrei Alexandrescu "C++ Modern Design" has a more lightweight solution.

He passes a type called Type2Type and is defined like so

  template <typename T>	struct	Type2Type{	typedef	T	Type;};// Use it likeF( Type2Type<X> );  


It''s more lightweight than a pointer or reference.

This topic is closed to new replies.

Advertisement