Operator << overloading

2019-01-27 09:25发布

I'm working on my project and I want to overload operator << which should print out my object on console.
Object is called "config" and in its template class it has 4 arrays called attribute1, attribute2. attribute3 and attribute4.

So far I've tried this :

#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;
}

Whenever I try to compile it, it gives some kind of weird error, but I know the problem is in the operator.

This is the error it gives :

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

and:

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

And line or column isnt specified.

1条回答
迷人小祖宗
2楼-- · 2019-01-27 09:41

Here's one way to solve the problem.

  1. Declare the operator<< function first, before config is defined. In order to declare the function, you have to forward declare the class template.

    template <typename T> class config;
    template <typename T> std::ostream& operator<<(std::ostream &out,
                                                   const config<T> &c);
    
  2. In the class, make the function a friend by using T as the template parameter.

    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>
    

Here's a working program with those changes:

#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;
}

Output:

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

查看更多
登录 后发表回答