【问题标题】:"not declared in scope" error in a header file with templates带有模板的头文件中的“未在范围内声明”错误
【发布时间】:2012-07-01 23:24:33
【问题描述】:

所以,我创建了一个包含以下内容的头文件:

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;
}
}

但是,编译时抛出错误,我不知道为什么。测试显然在范围内。

boost:ref(test&lt;a&gt;)&amp;test&lt;a&gt; 替换test&lt;a&gt; 仍然不起作用。

有什么想法吗?

【问题讨论】:

  • 你需要一个 test 的前向声明,就像你对任何其他函数一样。
  • 我不敢相信我错过了显而易见的......谢谢。

标签: c++ templates namespaces compiler-errors boost-function


【解决方案1】:

您需要至少声明某些东西才能使用它。在此之前编译器并不知道它确实存在。

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;
}
}

【讨论】:

  • 扩展这个答案,第 4-5 行的声明是test前向声明,即实际的初步声明 定义稍后在第 14-18 行说明。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
  • 2021-09-17
  • 2012-04-20
  • 1970-01-01
相关资源
最近更新 更多