Search code examples
c++templatesnamespacescompiler-errorsboost-function

"not declared in scope" error in a header file with templates


So, I create a header file that has the following:

namespace A
{

template<int a>
void foo(...)
{
    //This throws a "test was not declared in this scope" error:
    boost::function< bool (int, int)> t = test<a>; 
}

template<int a>
bool test(int c, int d)
{
    //Do stuff;
}
}

However, the error is thrown on compilation, and I don't know why. test is obviously in scope.

replacing test<a> with boost:ref(test<a>) or &test<a> still doesn't work.

Any ideas?


Solution

  • You need to atleast declare something before you can use it. The compiler doesn't know it actually exists before that.

    namespace A
    {
    
    template<int a>
    bool test(int c, int d);
    
    template<int a>
    void foo(...)
    {
        //This throws a "test was not declared in this scope" error:
        boost::function< bool (int, int)> t = test<a>; 
    }
    
    template<int a>
    bool test(int c, int d)
    {
        //Do stuff;
    }
    }