模板类的约束摸板友元函数问题
/* 想要在摸板类里面重载 operator < < 操作符,不知道为什么得到了编译器的强烈抱怨, 希望前辈们和各位高手指点: */
// 代码如下:
// A.h -- 头文件,定义模板.
#pragma once
#include <iostream>
template <typename TT>
std::ostream & operator < < (std::ostream &out, TT t);
template <class T, int n>
class A
{
private:
T m_ar[n];
int size;
public:
A() { size=n; }
A(T []);
~A(void);
const T & operator [] (int) const;
T & operator [] (int);
friend std::ostream & operator < < <> (std::ostream &out, const A <T,n> &t);
};
template <class T, int n>
A <T, n> ::A(T ar[])
{
if (NULL == ar)
{
return;
}
for (int i=0; i <n; ++i)
{
m_ar[i] = ar[i];
}
size = n;
}
template <class T, int n>
A <T, n> ::~A(void)
{
}
template <class T, int n>
const T & A <T, n> ::operator [] (int i) const
{
if (i <0 || i> =n)
{
exit(EXIT_FAILURE);
}
return m_ar[i];
}
template <class T, int n>
T & A <T, n> ::operator [] (int i)
{
if (i <0 || i> =n)
{
exit(EXIT_FAILURE);
}
return m_ar[i];
}
// 从这段代码开始有错误
template <typename TT>
std::ostream & operator < < (std::ostream &out, TT t)
{
for (int i=0; i <t.size; ++i)
{
std::cout < < t.m_ar[i] < < " ";
}
}
// test.cpp -- 测试文件
#include "A.h "
#include <iostream>
int main(void)
{
A <int, 2> a1;
A <A <int, 2> , 3> a2;
for (int i=0; i <2; ++i)
{
a1[i] = i;
}
for (int i=0; i <3; ++i)
{
for (int j=0; j <2; ++j)
{
a2[i][j] = (i+1) * (j+1);
}
}
std::cout < < a1 < < std::endl;
for (int i=0; i <3; ++i)
{
std::cout < < a2[i] < < std::endl;
}
return 0;
}
// 编译器愤怒了,它送给我这样的抱怨:
/*
e:\mywork\c++\templatetest\templatetest\A.h(65) : error C2248: “A <T,n> ::size”: 无法访问 private 成员(在“A <T,n> ”类中声明)
with
[
T=int,
n=2
]
e:\mywork\c++\templatetest\templatetest\A.h(13) : 参见“A <T,n> ::size”的声明
with
[
T=int,
n=2
]
.\test.cpp(19): 参见对正在编译的函数 模板 实例化“std::ostream &operator < < <A <T,n> > (std::ostream &,TT)”的引用
with
[
T=int,
n=2,
TT=A <int,2>
]
e:\mywork\c++\templatetest\templatetest\A.h(67) : error C2248: “A <T,n> ::m_ar”: 无法访问 private 成员(在“A <T,n> ”类中声明)
with
[
T=int,
n=2
]
e:\mywork\c++\templatetest\templatetest\A.h(12) : 参见“A <T,n> ::m_ar”的声明
with
[
T=int,
n=2
]
e:\mywork\c++\templatetest\templatetest\A.h(67) : error C2593: “operator < <”不明确
e:\mywork\c++\templatetest\templatetest\A.h(63): 可能是“std::ostream &operator < < <const char*> (std::ostream &,TT)”
with
[
TT=const char *
]
E:\Microsoft Visual Studio 8\VC\include\ostream(822): 或“std::basic_ostream <_Elem,_Traits> &std::operator < < <char,std::char_traits <char> > (std::basic_ostream <_Elem,_Traits> &,const _Elem *)”[使用参数相关的查找找到]
with
[
_Elem=char,
_Traits=std::char_traits <char>
]
E:\Microsoft Visual Studio 8\VC\include\ostream(735): 或“std::basic_ostream <_Elem,_Traits> &std::operator < < <std::char_traits <char> > (std::basic_ostream <_Elem,_Traits> &,const char *)”[使用参数相关的查找找到]
with
[
_Elem=char,
_Traits=std::char_traits <char>
]
E:\Microsoft Visual Studio 8\VC\include\ostream(650): 或“std::basic_ostream <_Elem,_Traits> &std::operator < < <char,std::char_traits <char> > (std::basic_ostream <_Elem,_Traits> &,const char *)”[使用参数相关的查找找到]
with
[
_Elem=char,
_Traits=std::char_traits <char>
]
试图匹配参数列表“(std::basic_ostream <_Elem,_Traits> , const char [2])”时
with
[
_Elem=char,
_Traits=std::char_traits <char>
]
*/
[解决办法]
应该参数声明错了:std::ostream & operator < < (std::ostream &out, const TT && t);
如果还不行,那就friend std::ostream & operator < <(std::ostream &out, const A <T,n> &t); 试试。
如果再不行,operator < < 的实现挪到A类内,在friend声明同时给出实现。
[解决办法]
支持taodm的说法。
[解决办法]
支持taodm的说法。