c++ - 运算符 << 重载

标签 c++ class templates generics operator-overloading

我正在做我的项目,我想重载运算符 <<它应该在控制台上打印出我的对象。
对象称为“config”,在其模板类中有 4 个数组,称为 attribute1 , attribute2 . attribute3attribute4 .

到目前为止我已经试过了:

#include "stdafx.h"
#include <string>
#include <iostream>

using namespace std;

template<typename T> class config
{
    T *attribute1, *attribute2, *attribute3, *attribute4;
    string attribName1, attribName2, attribName3, attribName4;
  public:
    config(void)
    {
      attribute1 = new T[3];
      attribute2 = new T[3];
      attribute3 = new T[3];
      attribute4 = new T[3];
    }   

    ~config(void)//destruktor
     {
       delete [] attribute1, attribute2, attribute3, attribute4;
     }

    //operatory
    friend ostream& operator<<(ostream &out, const config<T> &c); 
};//class ends

template <typename T> ostream& operator<<(ostream &out, const config<T> &c)
{
  for(int i=0;i<3;i++)
  {
    out<<c.attribute1[i]<<c.attribute2[i]<<c.attribute3[i]<<c.attribute2[i];
  }
  return out;
}

每当我尝试编译它时,它都会出现某种奇怪的错误,但我知道问题出在运算符上。

这是它给出的错误:

Error   1   error LNK2001: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class config<double> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$config@N@@@Z)    D:\Docs\Visual Studio 2010\Projects\project_v2.obj  project_v2

和:

Error   2   error LNK1120: 1 unresolved externals   D:\Docs\Visual Studio 2010\Projects\project_v2.exe  project_v2

未指定行或列。

最佳答案

这是解决问题的一种方法。

  1. 声明 operator<<函数优先,在config之前被定义为。为了声明该函数,您必须转发声明类模板。

    template <typename T> class config;
    template <typename T> std::ostream& operator<<(std::ostream &out,
                                                   const config<T> &c);
    
  2. 在类中,使用 T 将函数设为友元作为模板参数。

    friend ostream& operator<< <T>(ostream &out, const config &c);
    // Notice  this            ^^^^
    // This makes sure that operator<< <int> is not a friend of
    // config<double>. Only operator<< <double> is a friend of
    // config<double>
    

这是一个包含这些更改的工作程序:

#include <string>
#include <iostream>

template <typename T> class config;
template <typename T> std::ostream& operator<<(std::ostream &out,
                                               const config<T> &c);

template <typename T> class config
{
    T *attribute1, *attribute2, *attribute3, *attribute4;
    std::string attribName1, attribName2, attribName3, attribName4;
  public:
    config(void)
    {
      attribute1 = new T[3];
      attribute2 = new T[3];
      attribute3 = new T[3];
      attribute4 = new T[3];
    }   

    ~config(void)//destructor
     {
       delete [] attribute1;
       delete [] attribute2;
       delete [] attribute3;
       delete [] attribute4;
     }

    //operator
    friend std::ostream& operator<< <T>(std::ostream &out,
                                        const config &c);
};

template <typename T> std::ostream& operator<<(std::ostream &out,
                                               const config<T> &c)
{
   for(int i=0;i<3;i++)
   {
      out << c.attribute1[i] << " "
          << c.attribute2[i] << " "
          << c.attribute3[i] << " "
          << c.attribute2[i] << std::endl;
   }
   return out;
}

int main()
{
   config<double> x;
   std::cout << x << std::endl;
}

输出:

0 0 0 0
0 0 0 0
0 0 0 0

关于c++ - 运算符 << 重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29827631/

相关文章:

objective-c - 使用 extern @class 来添加类别?

c++ - 两个类和内联函数

html - 是否可以使用 flask 执行包含?

c++ - 非成员函数不推荐静态关键字的基本原理

c++ - 在无 block 模式下处理随机数据大小的接收

java - java中具有多个类的银行帐户客户端

c++ - 在 C++11 中将 PrintContainer 预处理器定义转换为模板

c++ - 重载函数模板和继承参数

c++ - 当错误存在时,为什么我的程序不执行第二个 catch block ?

c++ - SAL 注释,使用哪一个?