小编fel*_*lix的帖子

函数接受模板类的内部类的问题

我在类模板中的内部类有问题。我有一个模板类(比如Matrix<T>:)和一个子类(比如:)Matrix<T>::Row。现在我想编写一个对子类实例进行操作的函数(比如:)negate(Matrix<T>::Row &)。我试图用 声明函数template<class T> negate(typename Matrix<T>::Row &),但是当我尝试使用它时,编译器告诉我它找不到匹配项。

这是一个抽象的例子:

template<class T>
class A
{
public:
    class B
    {
    };
};

template<class T>
void x(typename A<T>::B &)
{
}

int main()
{
    A<int>::B b;
    x(b); // doesn't work: Error: Could not find a match
          // for x<T>(A<int>::B) needed in main().
    x<int>(b); // works fine
}
Run Code Online (Sandbox Code Playgroud)

为什么编译器无法x在第一种情况下找到?有没有办法修改它的工作原理(没有明确指定类型int)?

(我也有类似的问题 where xis of form template<class T, class S> void x(typename A<T>::B &, …

c++ templates

5
推荐指数
1
解决办法
353
查看次数

标签 统计

c++ ×1

templates ×1