C++中在类中重载输出运算符时遇到error: declaration of ‘class T‘的问题的解决

一、问题代码及报错提示:

​
#include <iostream>

using namespace std;

template<class T>
class Person{
public:
    template<class T>
    friend ostream& operator<<(ostream& out,Person<T> &p);
public:
    //重载输出运算符
    Person(T age,T id);
    void Show();
private:
    T mAge;
    T mID;
};
template<class T>
Person<T>::Person(T age,T id)
{
    this->mAge = age;
    this->mID = id;
}
template<class T>
void Person<T>::Show()
{
    cout<<"mAge:"<<this->mAge<<" "<<"mID"<<this->mID<<endl;
}
template<class T>
ostream& operator<<(ostream& out,Person<T> &p)
{
    out<<"mAge:"<<p.mAge<<" "<<"mID"<<p.mID<<endl;
    return out;
}

void test01()
{
    Person<int> p(20,10);
   // p.Show();
    cout<<p;

}

int main(int argc, char *argv[])
{
    test01();
    cout << "Hello World!" << endl;
    return 0;
}

​

 二、原因剖析

因为代码中用到类模板template<class T>而在类内声明的友元函数也用到了<T>,此时友元函数是依赖于类的实现的,所以编译器会报错

对此我们有几种解决方案:

1.改变友元与类模板的对应关系为多对多(若类模板使用的是template<class T>,友元函数就使用template<class U>来实现),代码如下:

#include <iostream>

using namespace std;

template<class T>
class Person{
public:
    template<class U>
    friend ostream& operator<<(ostream& out,Person<U> &p);
public:
    //重载输出运算符
    Person(T age,T id);
    void Show();
private:
    T mAge;
    T mID;
};
template<class T>
Person<T>::Person(T age,T id)
{
    this->mAge = age;
    this->mID = id;
}
template<class T>
void Person<T>::Show()
{
    cout<<"mAge:"<<this->mAge<<" "<<"mID"<<this->mID<<endl;
}
template<class U>
ostream& operator<<(ostream& out,Person<U> &p)
{
    out<<"mAge:"<<p.mAge<<" "<<"mID"<<p.mID<<endl;
    return out;
}

void test01()
{
    Person<int> p(20,10);
   // p.Show();
    cout<<p;

}

int main(int argc, char *argv[])
{
    test01();
    cout << "Hello World!" << endl;
    return 0;
}

2.改变友元与类模板的关系为一对一(朋友关系被限定在相同类型的实例化)代码如下:

#include <iostream>

using namespace std;
//前置声明
template<class T>
class Person;
template<class T>
ostream& operator<<(ostream& out,Person<T> &p);

template<class T>
class Person{
public:
    friend ostream& operator<<<T>(ostream& out,Person<T> &p);
public:
    Person(T age,T id);
    void Show();
private:
    T mAge;
    T mID;
};
template<class T>
Person<T>::Person(T age,T id)
{
    this->mAge = age;
    this->mID = id;
}
template<class T>
void Person<T>::Show()
{
    cout<<"mAge:"<<this->mAge<<" "<<"mID"<<this->mID<<endl;
}
template<class T>
ostream& operator<<(ostream& out,Person<T> &p)
{
    out<<"mAge:"<<p.mAge<<" "<<"mID"<<p.mID<<endl;
    return out;
}

void test01()
{
    Person<int> p(20,10);
   // p.Show();
    cout<<p;

}

int main(int argc, char *argv[])
{
    test01();
    cout << "Hello World!" << endl;
    return 0;
}

3.使用普通友元函数PrintPerson来实现,代码如下:

#include <iostream>

using namespace std;

template<class T>
class Person{
public:
    //普通友元函数的声明
    template<class U>
    friend void PrintPerson(Person<U>& p);
public:
    Person(T age,T id);
    void Show();
private:
    T mAge;
    T mID;
};
template<class T>
Person<T>::Person(T age,T id)
{
    this->mAge = age;
    this->mID = id;
}
template<class T>
void Person<T>::Show()
{
    cout<<"mAge:"<<this->mAge<<" "<<"mID"<<this->mID<<endl;
}
//普通友元函数的实现
template<class U>
void PrintPerson(Person<U>& p)
{
    cout<<"mAge:"<<p.mAge<<" "<<"mID"<<p.mID<<endl;
}
void test01()
{
    Person<int> p(20,10);
   // p.Show();
    //cout<<p;
    PrintPerson(p);

}

int main(int argc, char *argv[])
{
    test01();
    cout << "Hello World!" << endl;
    return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zkaisen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值