0

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?

Andrew Spott
  • 3,457
  • 8
  • 33
  • 59

1 Answers1

3

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;
}
}
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • Expanding on this answer, the statement at lines 4-5 is the **forward declaration** of `test`, i.e. a preliminary **declaration** for the actual **definition** that is stated later, at lines 14-18. – Marco Leogrande Jul 02 '12 at 00:52