Sign in to follow this  
PureSnowX

Problem with templates

Recommended Posts

PureSnowX    275
I'v been trying to delve into templates using C++, I thought the Locator pattern was excellent for this but I'm having some problems with compiling the following code:

Locator.hpp
[CODE]
template<class T, class B>
class Locator
{
public:
static void provide(T* service)
{
if( service = NULL )
Locator<T,B>::mService = &Locator<T,B>::nullService;
else
Locator<T,B>::mService = service;
}
static T* service()
{
if( service == NULL )
{
Locator<T,B>::mService = &Locator<T,B>::nullService;
return Locator<T,B>::mService;
}
else
{
Locator<T,B>::mService = service;
return Locator<T,B>::mService;
}
}
private:
static T* mService;
static B nullService;
};
[/CODE]

It specifies what class to locate and what class to use as a null-class when the locator is null. But I'm getting some weird assignment problem like: Locator<T,B>::mService = service can't assign because *AudioService can convert to *AudioService

Share this post


Link to post
Share on other sites
Brother Bob    10344
The line you mentioned occurs only in [i]Locator::service()[/i], and in that context, the symbol [i]service[/i] is a function pointer to [i]Locator::service()[/i] and thus not assignable to the type of the member [i]mService[/i]. Furthermore, in Locator::provide(), you're not comparing the argument to null in the if-statement, but you're assigning to it.

Never paraphrase an error message, you should always copy and paste them as they are, because getting even a small detail wrong or missing can greatly change the meaning of the error and the interpretation of the situation. You may then get the wrong answer because we assumed the wrong situation about your code.

Share this post


Link to post
Share on other sites
PureSnowX    275
[quote name='Brother Bob' timestamp='1352303098' post='4998442']
The line you mentioned occurs only in [i]Locator::service()[/i], and in that context, the symbol [i]service[/i] is a function pointer to [i]Locator::service()[/i] and thus not assignable to the type of the member [i]mService[/i]. Furthermore, in Locator::provide(), you're not comparing the argument to null in the if-statement, but you're assigning to it.

Never paraphrase an error message, you should always copy and paste them as they are, because getting even a small detail wrong or missing can greatly change the meaning of the error and the interpretation of the situation. You may then get the wrong answer because we assumed the wrong situation about your code.
[/quote]

EDIT: It gave me compiling errors, nvm.
These are the errors:
[quote]
Game.obj : error LNK2001: unresolved external symbol "private: static class AudioService * Locator<class AudioService,class NullAudio>::mService" (?mService@?$Locator@VAudioService@@VNullAudio@@@@0PAVAudioService@@A)
1>Game.obj : error LNK2001: unresolved external symbol "private: static class NullAudio Locator<class AudioService,class NullAudio>::nullService" (?nullService@?$Locator@VAudioService@@VNullAudio@@@@0VNullAudio@@A)
[/quote]

and here is the current code:
[CODE]
template<class T, class B>
class Locator
{
public:
static void Locator<T,B>::provide(T* service)
{
if( service == NULL )
Locator<T,B>::mService = &Locator<T,B>::nullService;
else
Locator<T,B>::mService = service;
}
static T* service()
{
if( Locator<T,B>::mService == NULL )
return &Locator<T,B>::nullService;
else
return Locator<T,B>::mService;
}
private:
static T* mService;
static B nullService;
};
[/CODE] Edited by Moonkis

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this