I'm new to GCC and am trying to port code to Linux which compiled OK with MSVC. The code below (a small program wich can be copypasted and compiled) produces the error:
"there are no arguments to ‘num_obj_inc’ that depend on a template parameter, so a declaration of ‘num_obj_inc’ must be available [-fpermissive]"
#include <iostream>
using namespace std;
#define _SPTRDBG
#ifdef _SPTRDBG
#define SPTRDBG(x) x;
#else
#define SPTRDBG(x)
#endif
template<bool>
struct objCounter
{
void num_obj_inc(){}
void num_obj_dec(){}
};
template<>
struct objCounter<true>
{
#ifdef _SPTRDBG
static int num;
void num_obj_inc() { num++; }
void num_obj_dec() { num--; }
#endif
};
template<class C, bool bCnt=false>
class SPtr
: public objCounter<bCnt>
{
C* p;
public:
SPtr(C *_p)
:p(_p)
{
if ( p ) {
SPTRDBG( num_obj_inc() )
}
}
};
int main()
{
return 0;
}
My guess was that GCC somehow optimize out the noop "num_obj_inc(){}", but this code compiles OK:
struct objCounter
{
void num_obj_inc(){}
void num_obj_dec(){}
};
class SPtr
: public objCounter
{
public:
SPtr(int *p)
//:p(_p)
{
if ( p ) {
SPTRDBG( num_obj_inc() )
}
}
};
What can be cause of the compilation error?
You need to use:
this->num_obj_inc();
See Why do I have to access template base class members through the this pointer? for detailed explanation.