【发布时间】:2012-01-21 22:53:41
【问题描述】:
#include <iostream>
using namespace std;
template <typename T>
class test
{
T y;
public:
test(T k) : y(k) {}
friend int a(T& x);
};
template <typename T>
int a(T& x)
{
cout << x.y;
return 9;
}
template <>
class test<int>
{
int y;
public:
test(int k) : y(k) {}
friend int a(int& x);
};
template <>
int a<int>(int& x)
{
cout << "4";
return 0;
}
int main(int argc, char* argv[])
{
test<int> z(3);
a(z);
return 0;
}
我想结交一个测试类的朋友类(在实际案例中,它是 ofstream 的 operator
此外,上面的代码显示了这个编译错误信息;
错误 C2248: 'test::y' : 无法访问声明的私有成员 类'测试'
已添加问题;
Aaron McDaid 对我来说工作得很好,但我试图重载 ofstream 类的 operator
friend ofstream& operator<< <test<int>> (ofstream& os, const test<int>& t);
我在上面添加了代码来测试类和
template<>
ofstream& operator<< <test<int> > (ofstream& os, const test<int>& t)
{
os << t.y;
return os;
}
使用上面的代码。但看起来我不能使用 os int) 我不明白为什么会这样。错误信息是
错误 C2027:使用未定义类型 'std::basic_ofstream<_elem>'
【问题讨论】:
标签: c++ templates compiler-errors friend template-specialization