• Create Account

## Problem with templates

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1Moonkis  Members

275
Like
0Likes
Like

Posted 07 November 2012 - 09:25 AM

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
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;
};


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

### #2Brother Bob  Moderators

10108
Like
2Likes
Like

Posted 07 November 2012 - 09:44 AM

The line you mentioned occurs only in Locator::service(), and in that context, the symbol service is a function pointer to Locator::service() and thus not assignable to the type of the member mService. 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.

### #3Moonkis  Members

275
Like
0Likes
Like

Posted 07 November 2012 - 10:11 AM

The line you mentioned occurs only in Locator::service(), and in that context, the symbol service is a function pointer to Locator::service() and thus not assignable to the type of the member mService. 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.

EDIT: It gave me compiling errors, nvm.
These are the errors:

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)

and here is the current 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;
};


Edited by Moonkis, 07 November 2012 - 10:17 AM.

### #4mrbastard  Members

1576
Like
0Likes
Like

Posted 07 November 2012 - 11:00 AM

Those are linker errors. There is no definition (note: definition as opposed to declaration) for the static members of your class.

### #5Brother Bob  Moderators

10108
Like
0Likes
Like

Posted 07 November 2012 - 11:00 AM

Static member variables has to be defined.
template<typename T, typename B> T* Locator<T,B>::mService = nullptr;
template<typename T, typename B> B Locator<T,B>::nullService;


Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.