Raised This Month: $ Target: $400
 0% 

template functions question


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
sn4k3
Senior Member
Join Date: Nov 2005
Old 01-03-2008 , 11:52   template functions question
Reply With Quote #1

i have in .cpp:

Code:
template <typename T> T& SUtilsLib::Property(CBaseEntity* pBase, const char* pClass, const char* pProp)
{ 
    return *reinterpret_cast<T*>(reinterpret_cast<char*>(pBase) + FindOffset(pClass, pProp)); 
} 

template <typename T> T& SUtilsLib::Property(CBaseEntity* pBase, int index)
{ 
    return *reinterpret_cast<T*>(reinterpret_cast<char*>(pBase) + index); 
}
in .h

Code:
class SUtilsLib
{
public:
	//...
	// Set or Get an Property by name.
	template <typename T> T& Property(CBaseEntity* pBase, const char* pClass, const char* pProp);

	// Set or Get an Property by index.
	template <typename T> T& Property(CBaseEntity* pBase, int index);
	//...
};
when i try useit like:

Code:
spt_utils.Property<string_t>(pWeapon, td->fieldOffset[0]) = MAKE_STRING("new_weapon_ak47");
give unresolved external problems in VS2005.

all others funcs in spt_utils works well.

this also happen when i use inline functions in a class

how can i put that in a class without give that errors any c++ option?

thanks
sn4k3 is offline
Send a message via MSN to sn4k3
BAILOPAN
Join Date: Jan 2004
Old 01-03-2008 , 14:18   Re: template functions question
Reply With Quote #2

Unfortunately I don't think there is a nice way. Templates are weird and different code gets generated for each type, so the template implementation must be visible to everyone (that is, in the .h file).
__________________
egg
BAILOPAN is offline
PM
hello, i am pm
Join Date: Jan 2004
Location: Canalization
Old 01-03-2008 , 16:01   Re: template functions question
Reply With Quote #3

There is an export keyword to allow this but very few compilers support it (the compilers that are commonly used for HL programming don't).

More info here.

Just do what BAIL said and include all the template stuff in a header file.
__________________
hello, i am pm
PM is offline
Saul
Junior Member
Join Date: Jun 2007
Old 01-03-2008 , 18:53   Re: template functions question
Reply With Quote #4

Arrr! You have stolen my templated entity prop code (that Nicolous gave me). I use this code in my .h file:

Code:
#ifndef _TEMPLATES_
#define _TEMPLATES_
template<typename T>
T UTIL_EntityProp(CBaseEntity* pBase, const char* sdkClass, const char* prop)
{
    // Get the offset
    int offset = UTIL_FindOffset(sdkClass, prop);

    // Check the offset
    if(offset == 0)
    {
        LogString("EntityProp", "Cannot retrieve offset for: %s.%s", sdkClass, prop);
        return NULL;
    }

    // Return
    return *reinterpret_cast<T*>(reinterpret_cast<char*>(pBase) + offset);
};
#endif
Saul is offline
Send a message via MSN to Saul
sn4k3
Senior Member
Join Date: Nov 2005
Old 01-03-2008 , 19:15   Re: template functions question
Reply With Quote #5

Quote:
Originally Posted by Saul View Post
Arrr! You have stolen my templated entity prop code (that Nicolous gave me). I use this code in my .h file:

Code:
#ifndef _TEMPLATES_
#define _TEMPLATES_
template<typename T>
T UTIL_EntityProp(CBaseEntity* pBase, const char* sdkClass, const char* prop)
{
    // Get the offset
    int offset = UTIL_FindOffset(sdkClass, prop);

    // Check the offset
    if(offset == 0)
    {
        LogString("EntityProp", "Cannot retrieve offset for: %s.%s", sdkClass, prop);
        return NULL;
    }

    // Return
    return *reinterpret_cast<T*>(reinterpret_cast<char*>(pBase) + offset);
};
#endif
i have already it in SPT but i not use template in
so i have tryed implement template for make easy return types

template edia was given to me by 'Wuh'
sn4k3 is offline
Send a message via MSN to sn4k3
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 06:08.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode