1

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?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Al Berger
  • 1,048
  • 14
  • 35
  • 2
    Is there any particular reason you didn't remove the `_SPTRDBG` nonsense from your code? Or does the bug depend on that remaining? I've attempted to reduce your example to a smaller test which you can see at [this gist](https://gist.github.com/sharth/3a9e119021c83cca4031). If that also still exhibits the bug, you should update your question with that code. – Bill Lynch May 27 '14 at 03:31
  • Have you tried adding `-fpermissive` to your compiler flags? – Elliott Frisch May 27 '14 at 03:33
  • @sharth - I didn't know for sure that the _SPTRDBG macros don't anything to with the error (template expansion and preproprecessing both occure before compilation). Anyways, your link provided the answer. Thanks. – Al Berger May 27 '14 at 03:49
  • Either you or @sharth should provide an answer then ... – πάντα ῥεῖ May 27 '14 at 03:52
  • Template expansion is most definitely part of compilation proper... – Drew Hall May 27 '14 at 04:00

1 Answers1

2

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.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270