Archived

This topic is now archived and is closed to further replies.

dabutabey

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

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
[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<type_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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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!]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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<print_job> print_queue; 





I wanna work for Microsoft!

Share this post


Link to post
Share on other sites
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 like

F( Type2Type<X> );


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

Share this post


Link to post
Share on other sites
quote:
Original post by Null and Void
Oluseyi, it didn''t work for you . Next time I''m writing it out by hand in HTML ( ''s and >''s, heh) without any of the broken code formating thingies.

The stupid part is that if you click "edit", you can usually see how someone formatted something. Not so with source and the angle brackets. It actually converts all the separate source pairs to a single pair (so be careful when you edit your own source! I generally use [ code ] these days.) Similarly, when you use & lt and & gt in your original post, it converts them to "<" and ">".

Ah, message boards...



I wanna work for Microsoft!

Share this post


Link to post
Share on other sites