R
Ruben Campos
Greetings. Please, take a look to the next code, where I have a problem with
specialization of class member functions.
// ########## CPrinter.h ##########
#ifndef PRINTER_H
#define PRINTER_H
enum TPrintPolicy { POLICY1 = 0, POLICY2 };
template <typename T>
class CPrinter
{
public:
template <TPrintPolicy P> void Print (T const value);
};
#include "CPrinter.cpp"
#endif
// ########## CPrinter.cpp ##########
#include <iostream>
using std::cout;
using std::endl;
template <typename T>
template <TPrintPolicy P>
void
CPrinter <T>:rint <P> (T const value)
{
cout << "CPrinter <T>:rint <P> prints " << value << " value." << endl;
}
template <typename T>
template <>
void
CPrinter <T>:rint <POLICY1> (T const value)
{
cout << "CPrinter <T>:rint <POLICY1> prints " << value << " value." <<
endl;
}
// ########## MyClass.h ##########
#ifndef MY_CLASS_H
#define MY_CLASS_H
class MyClass
{
public:
void Foo ();
};
#endif
// ########## MyClass.cpp ##########
#include "MyClass.h"
#include "CPrinter.h"
void
MyClass::Foo ()
{
CPrinter <int> printer;
printer.Print <POLICY2> (5);
}
// ########## main.cpp ##########
#include "MyClass.h"
#include "CPrinter.h"
int
main (int argn, char ** argv)
{
CPrinter <int> printer;
printer.Print <POLICY1> (4);
MyClass x;
x.Foo();
return 0;
}
I've tried this with Microsoft Visual C++ .NET (7.x). If I comment the
CPrinter <T>:rint <POLICY1> specialized implementation (in CPrinter.cpp),
this source builds and runs fine. However, compiler returns errors when
including that specialization of CPrinter <T>:rint member function. As a
remarkable note, I've noticed that the number of errors returned by the
compiler increases when changing the order of the two #include in the
main.cpp file, with new "MyClass not defined" like errors.
Could someone say me what is happening here? Is there any right way to
specialize the template CPrinter <T>:rint method?
Thank you vey much in advance.
specialization of class member functions.
// ########## CPrinter.h ##########
#ifndef PRINTER_H
#define PRINTER_H
enum TPrintPolicy { POLICY1 = 0, POLICY2 };
template <typename T>
class CPrinter
{
public:
template <TPrintPolicy P> void Print (T const value);
};
#include "CPrinter.cpp"
#endif
// ########## CPrinter.cpp ##########
#include <iostream>
using std::cout;
using std::endl;
template <typename T>
template <TPrintPolicy P>
void
CPrinter <T>:rint <P> (T const value)
{
cout << "CPrinter <T>:rint <P> prints " << value << " value." << endl;
}
template <typename T>
template <>
void
CPrinter <T>:rint <POLICY1> (T const value)
{
cout << "CPrinter <T>:rint <POLICY1> prints " << value << " value." <<
endl;
}
// ########## MyClass.h ##########
#ifndef MY_CLASS_H
#define MY_CLASS_H
class MyClass
{
public:
void Foo ();
};
#endif
// ########## MyClass.cpp ##########
#include "MyClass.h"
#include "CPrinter.h"
void
MyClass::Foo ()
{
CPrinter <int> printer;
printer.Print <POLICY2> (5);
}
// ########## main.cpp ##########
#include "MyClass.h"
#include "CPrinter.h"
int
main (int argn, char ** argv)
{
CPrinter <int> printer;
printer.Print <POLICY1> (4);
MyClass x;
x.Foo();
return 0;
}
I've tried this with Microsoft Visual C++ .NET (7.x). If I comment the
CPrinter <T>:rint <POLICY1> specialized implementation (in CPrinter.cpp),
this source builds and runs fine. However, compiler returns errors when
including that specialization of CPrinter <T>:rint member function. As a
remarkable note, I've noticed that the number of errors returned by the
compiler increases when changing the order of the two #include in the
main.cpp file, with new "MyClass not defined" like errors.
Could someone say me what is happening here? Is there any right way to
specialize the template CPrinter <T>:rint method?
Thank you vey much in advance.