Sha*_*man 7 c++ parameters templates
我正在努力使用以下代码.基本上,我有一个类Foo和嵌套类Bar,现在我想将一个类Bar对象的指针传递给一个函数,但它不能编译.任何人都可以帮我这个吗?谢谢.
template <typename T>
struct Foo
{
struct Bar
{
T data_;
};
Bar bar_;
};
template <typename T>
void func(Foo<T>::Bar* bar) // Why is this line wrong???
{
}
int main()
{
Foo<int> foo;
foo.bar_.data_ = 17;
func(&foo.bar_);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
jos*_*mas 15
您需要具有以下签名
template <typename T>
void func(typename Foo<T>::Bar* bar) // Why is this line wrong???
Run Code Online (Sandbox Code Playgroud)
但是,这不是唯一的问题
func(&foo.bar_);
Run Code Online (Sandbox Code Playgroud)
也需要
func<int>(&foo.bar_);
Run Code Online (Sandbox Code Playgroud)
这是因为您正在调用模板化函数"func",但无法推断其类型.没有它的类型,它会给出一个错误,例如
no matching function for call to 'func(Foo<int>::Bar*)'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10037 次 |
| 最近记录: |