Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Error :: specialization of Template Function on Meta - Template Class

Reply
Thread Tools

Error :: specialization of Template Function on Meta - Template Class

 
 
Pallav singh
Guest
Posts: n/a
 
      01-06-2012
Hi All ,

i am getting Error while specialization of Template Function on Meta -
Template Class
Thanking in Advance.

------------------ file1.h -----------------------------------
template <typename T>
class Foo {
T data;
public:
Foo(T in);

template <typename TT> void bar(TT value);
};

=========== End of File =============
-------------------------file1.hpp -----------------------------

#include "file1.h"

#include <iostream>
#include <string>
using namespace std;

template <typename T>
Foo<T> :: Foo(T input) : data(input) { }

template <typename T>
template <typename TT>
void Foo<T> :: bar(TT value)
{
cout << " Foo<T> :: void bar(TT value) : " << value << endl;
}


template <typename T>
void Foo<T> :: bar<string>(string value)
{
cout << " Foo<T> :: String Specialization : " << value << endl;
}

=============================================

----------------------------------- file1.cpp
----------------------------------------

#include "file1.h"

#include <iostream>
using namespace std;

template <>
class Foo<int> {
int data;

public:
Foo(int in) : data(in) { }

template<typename TT>
void bar(TT value) {
cout << "default: " << static_cast<int>(value)*data << endl;
}

};

template <>
void Foo<int> :: bar(double value) {
cout << "special: " << value*data << endl;
}

==========================================

----------------------- ExportFile.cc
--------------------------------------
#include "file1.hpp"

#include <string>
using namespace std;

//////// Specialization part //////////
template class Foo<int>;

template class Foo<string>;
template class Foo<float>;
template class Foo<char>;


// Explicit generation of Function Symbol
// Explicit Instantation of Function inside Class

template void Foo<char> :: bar<char> ( char );
template void Foo<int> :: bar<int> ( int );
template void Foo<float> :: bar<float> ( float );
template void Foo<string> :: bar<string> ( string )

================================================

$ g++ -c -g file1.cc ExportFile.cc
In file included from ExportFile.cc:1:0:
file1.hpp:19:41: error: template-id `bar<std::string>' in declaration
of primary
template
file1.hpp:19:7: error: prototype for `void Foo<T>::bar(std::string)'
does not ma
tch any in class `Foo<T>'
file1.h:7:45: error: candidate is: template<class T> template<class
TT> void Foo
::bar(TT)


Thanks
Pallav Singh




 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-06-2012
On 1/6/2012 1:18 PM, Pallav singh wrote:
> Hi All ,
>
> i am getting Error while specialization of Template Function on Meta -
> Template Class
> Thanking in Advance.
>
> ------------------ file1.h -----------------------------------
> template<typename T>
> class Foo {
> T data;
> public:
> Foo(T in);
>
> template<typename TT> void bar(TT value);
> };
>
> =========== End of File =============
> -------------------------file1.hpp -----------------------------
>
> #include "file1.h"
>
> #include<iostream>
> #include<string>
> using namespace std;
>
> template<typename T>
> Foo<T> :: Foo(T input) : data(input) { }
>
> template<typename T>
> template<typename TT>
> void Foo<T> :: bar(TT value)
> {
> cout<< " Foo<T> :: void bar(TT value) : "<< value<< endl;
> }
>
>
> template<typename T>
> void Foo<T> :: bar<string>(string value)
> {
> cout<< " Foo<T> :: String Specialization : "<< value<< endl;
> }
>
> =============================================
>
> ----------------------------------- file1.cpp
> ----------------------------------------
>
> #include "file1.h"
>
> #include<iostream>
> using namespace std;
>
> template<>
> class Foo<int> {
> int data;
>
> public:
> Foo(int in) : data(in) { }
>
> template<typename TT>
> void bar(TT value) {
> cout<< "default: "<< static_cast<int>(value)*data<< endl;
> }
>
> };
>
> template<>
> void Foo<int> :: bar(double value) {
> cout<< "special: "<< value*data<< endl;
> }
>
> ==========================================
>
> ----------------------- ExportFile.cc
> --------------------------------------
> #include "file1.hpp"
>
> #include<string>
> using namespace std;
>
> //////// Specialization part //////////
> template class Foo<int>;
>
> template class Foo<string>;
> template class Foo<float>;
> template class Foo<char>;
>
>
> // Explicit generation of Function Symbol
> // Explicit Instantation of Function inside Class
>
> template void Foo<char> :: bar<char> ( char );
> template void Foo<int> :: bar<int> ( int );
> template void Foo<float> :: bar<float> ( float );
> template void Foo<string> :: bar<string> ( string )
>
> ================================================
>
> $ g++ -c -g file1.cc ExportFile.cc
> In file included from ExportFile.cc:1:0:
> file1.hpp:19:41: error: template-id `bar<std::string>' in declaration
> of primary
> template
> file1.hpp:19:7: error: prototype for `void Foo<T>::bar(std::string)'
> does not ma
> tch any in class `Foo<T>'
> file1.h:7:45: error: candidate is: template<class T> template<class
> TT> void Foo
> ::bar(TT)


You cannot specialize a member template without first specializing the
class template. As soon as you wrote

template<class T> void Foo<T>::bar<string>

you've violated that rule.

V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Full specialization of a member function template of a class template Dave C++ 4 06-04-2010 12:15 PM
template function specialization inside template class possible? quarup@gmail.com C++ 7 08-25-2006 11:01 PM
specialization of a template function in a template class?!?!? pookiebearbottom@yahoo.com C++ 2 08-07-2006 01:08 PM
Specialization of member function template in template class? Joseph Turian C++ 4 03-20-2006 10:07 AM
Member template function specialization in a template class Ruben Campos C++ 3 02-15-2005 10:40 AM



Advertisments