我想創建一個模板類與嵌套命名空間內的朋友功能。如果我刪除所有的命名空間,或者刪除了所有的模板化,它會正常工作。但是兩者都不能編譯。讓我們來看看一些代碼:模板類與嵌套命名空間內的朋友功能
namespace MyNamespace
{
// Forward declaration
template <typename Type>
class Container;
// Forward declaration
namespace AccessPrivateImplementation
{
template <typename Type>
Type getValue(Container<Type>* container);
}
// Templatized class
template <typename Type>
class Container
{
friend Type AccessPrivateImplementation::getValue(Container<Type>* volume);
private:
Type value;
};
// Friend function inside a namespace
namespace AccessPrivateImplementation
{
template <typename Type>
Type getValue(Container<Type>* container)
{
return container->value;
}
}
}
int main(int argc, char* argv[])
{
MyNamespace::Container<int> cont;
MyNamespace::AccessPrivateImplementation::getValue(&cont);
return 0;
}
編譯器(VS2010)告訴我:
錯誤C2248: '了myNameSpace ::集裝箱::值':不能訪問類的了myNameSpace聲明私有成員: :容器'
有沒有人有任何想法我失蹤?
儘量宣佈之前''Container' getValue'。 – Pubby 2013-04-30 19:50:01
This thread may help http://stackoverflow.com/questions/16288446/i-need-help-fixing-an-error-message-error-lnk2019-unresolved-external-symbol/16288486#16288486 – 2013-04-30 19:51:02
@Pubby - 謝謝,但getValue需要第二個,因爲它需要一個指向Container的指針。 – 2013-04-30 20:08:13