Archived

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

D

Returning Templates

Recommended Posts

Heya, I''m having a serious problem here, I try to return a template class from a function and it all goes to hell. Basically I am trying to do this:
TArray& ListFiles( /* blah */ );
or this:
int ListFiles( TArray* /* blah */ );
 
and none of them return the Array Object properly. I.e. its all fuX0rd... I''m using MSVC 6.0 BTW and so I am wondering if this is a compiler bug, template bug, or that I have to do something special (like make a good Copy constructor?). -Cheers. P.S. I''ve already got ideas to make a wrapper class for TArray and my string class works okay.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The problem is with your code; it''s not a compiler bug.

Try something like this:


template
TArray& ListFiles(/* blah */) {...}

// Or...

template
int ListFiles(TArray *t, /* blah */);

// Note: you may have to call your function like this:
// TArray t = ListFiles(/* blah */);


Hope this helps.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sorry.... Let''s try this again, shall we?

template < typename T >
TArray< T >& ListFiles(/* blah */) {...}

// Or...

template < typename T >
int ListFiles(TArray< T > *t, /* blah */);

// Note: you may have to call your function like this:
// TArray< int > t = ListFiles< int >(/* blah */);

Share this post


Link to post
Share on other sites
Well the thing is that TArray, and BString are already defined classes, and that particular function is defined within another class.
i.e.

template class TArray { ... };
// and
class BString { ... };
// etc


Strickly speaking

TArray&

is just the return type.

And the way that I''m calling it is like so:


TArray Stuff = Something.ListFiles(/* blah */);


It still stuffs up then, so I am really wondering if its got anything to do with the = operator. Its just wierd...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It appears your templates got cut off (because this board seems to interpret angular brackets as HTML tags).

Anyway, you can''t do something like this:

TArray t = ...;

You have to give TArray a type. So it''d be like this:

  
TArray<int> t = Something.ListFiles(/* blah */);


You have to specify a type for the template class when you call it.

Also, if the return type is "TArray&" (and not "TArray< T >&"), and TArray is a template then that code is probably wrong. It needs to look like:

template< typename T >
TArray< T >& Something::ListFiles(/* blah */) { ... }

Share this post


Link to post
Share on other sites
Yeah it got cut off, basically I have done everything that you have suggested it before, (I''m not that much of an idiot you know).

Oh well I''ll just make a wrapper class or something.

Share this post


Link to post
Share on other sites