|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
On Oct 14, 12:38 pm, mike b <michaeljber...@gmail.com> wrote:
> Hello everyone, thanks in advance for your . I'm new to C++ > templates and have run into some issues using member function > templates. I have a shared library containing templates that I'm > trying to use from an executable, compile using gcc 4.1.2. Everything > works fine until I try specializing one of the static member function > templates in a non-template class. I have a feeling I'm messing up > something obvious so before I post a bunch of code does the following > look correct? Thanks. > > --- Shared Library (Arrays.h) --- > > namespace example { > class Arrays { > public: > template<class T> static int compareItems(T *a1, T *a2, int length); > protected: > Arrays() {}; > > }; > > template<class T> int Arrays::compareItems(T *a1, T *a2, int length) { > printf("Calling generic function.\n"); > return 1; > > } > > template<> int Arrays::compareItems<char>(char *a1, char *a2, int > length) { > printf("Calling char specialization.\n"); > return 0; > > } > } Sincere thanks to everyone who responded, the problem has been solved. The issue it seems is that I was defining my member specializations in the header but only a declaration for the specialization is allowed in the header, the actual implementation must go in the implementation file, .cpp. So here is the change to make the code work: --- Arrays.h --- namespace example { class Arrays : public Object { public: template<class T> static int compareItems(const T a1[], const T a2[], const int length); }; // I was defining the function here but only the declaration goes here template<> int Arrays::compareItems(const char a1[], const char a2[], const int length); template<class T> int compareItems(const T a1[], const T a2[], const int length) { // Generic implementation goes here. } } --- Arrays.cpp --- // This was in the header file before and was causing the problems, moving it here solved it. template<> int hydro::Arrays::compareItems(const char a1[], const char a2[], const int length) { return strncmp(a1, a2, length); } --- END --- |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
mike b wrote:
.... > Sincere thanks to everyone who responded, the problem has been > solved. The issue it seems is that I was defining my member > specializations in the header but only a declaration for the > specialization is allowed in the header, the actual implementation > must go in the implementation file, .cpp. Sounds strange. What errors were you getting ? |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Oct 16, 3:45 pm, Gianni Mariani <gi4nos...@marian.ws> wrote:
> mike b wrote: > > ... > > > Sincere thanks to everyone who responded, the problem has been > > solved. The issue it seems is that I was defining my member > > specializations in the header but only a declaration for the > > specialization is allowed in the header, the actual implementation > > must go in the implementation file, .cpp. > > Sounds strange. What errors were you getting ? I was getting errors about my specialization being defined multiple times. This was with GCC 4. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
mike b wrote:
> On Oct 16, 3:45 pm, Gianni Mariani <gi4nos...@marian.ws> wrote: >> mike b wrote: >> >> ... >> >>> Sincere thanks to everyone who responded, the problem has been >>> solved. The issue it seems is that I was defining my member >>> specializations in the header but only a declaration for the >>> specialization is allowed in the header, the actual implementation >>> must go in the implementation file, .cpp. >> Sounds strange. What errors were you getting ? > > I was getting errors about my specialization being defined multiple > times. This was with GCC 4. Declare the template as "inline" as see if you get the same errors. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
On Oct 17, 1:17 am, Gianni Mariani <gi4nos...@marian.ws> wrote:
> mike b wrote: > > On Oct 16, 3:45 pm, Gianni Mariani <gi4nos...@marian.ws> wrote: > >> mike b wrote: > > >>> Sincere thanks to everyone who responded, the problem has been > >>> solved. The issue it seems is that I was defining my member > >>> specializations in the header but only a declaration for the > >>> specialization is allowed in the header, the actual implementation > >>> must go in the implementation file, .cpp. > >> Sounds strange. What errors were you getting ? > > > I was getting errors about my specialization being defined multiple > > times. This was with GCC 4. > > Declare the template as "inline" as see if you get the same errors. An explicit function specialization follows the same rules as any other C++ function: in order to be defined in a header file, the specialization must be declared "inline." Greg |
|
![]() |
| Outils de la discussion | |
|
|