超载的朋友操作<<模板类

问题描述 投票:48回答:5

我已经阅读了有关我对StackOverflow.com问题夫妇的问题,现在,没有它似乎解决我的问题。或者,我也许已经做错了...重载<<作品,如果我把它变成一个内联函数。但我怎么让它在我的情况下工作吗?

warning: friend declaration std::ostream& operator<<(std::ostream&, const D<classT>&)' declares a non-template function

warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning

/tmp/cc6VTWdv.o:uppgift4.cc:(.text+0x180): undefined reference to operator<<(std::basic_ostream<char, std::char_traits<char> >&, D<int> const&)' collect2: ld returned 1 exit status

编码:

template <class T>
T my_max(T a, T b)
{
   if(a > b)      
      return a;
   else
      return b;
}

template <class classT>
class D
{
public:
   D(classT in)
      : d(in) {};
   bool operator>(const D& rhs) const;
   classT operator=(const D<classT>& rhs);

   friend ostream& operator<< (ostream & os, const D<classT>& rhs);
private:
   classT d;
};


int main()
{

   int i1 = 1;
   int i2 = 2;
   D<int> d1(i1);
   D<int> d2(i2);

   cout << my_max(d1,d2) << endl;
   return 0;
}

template <class classT>
ostream& operator<<(ostream &os, const D<classT>& rhs)
{
   os << rhs.d;
   return os;
}
c++ templates operator-overloading friend ostream
5个回答
143
投票

这是那些具有相似但不是真的一样不同的方法常见的问题之一。这三种方法你是谁宣称是你的功能的朋友--and那么你如何实现它的不同。

性格外向

声明模板作为朋友的所有实例。这就是你接受的答案,也是大多数其他答案的建议。在这种方法中,你是不必要通过声明的朋友们都D<T>实例打开您的具体实例operator<<。也就是说,std::ostream& operator<<( std::ostream &, const D<int>& )访问D<double>的所有内部。

template <typename T>
class Test {
   template <typename U>      // all instantiations of this template are my friends
   friend std::ostream& operator<<( std::ostream&, const Test<U>& );
};
template <typename T>
std::ostream& operator<<( std::ostream& o, const Test<T>& ) {
   // Can access all Test<int>, Test<double>... regardless of what T is
}

该内向

只有声明插入运营商的朋友的特定实例。当应用于自身D<int>可以像插入操作,但它不希望任何与std::ostream& operator<<( std::ostream&, const D<double>& )

这可以通过两种方式来完成,简单的方法是为@Emery伯杰提出的,这是内联的运营商,可呈现也是其他原因是一个好主意:

template <typename T>
class Test {
   friend std::ostream& operator<<( std::ostream& o, const Test& t ) {
      // can access the enclosing Test. If T is int, it cannot access Test<double>
   }
};

在这第一个版本,你是不是创建一个模板operator<<,而是为Test模板的每个实例化一个非模板函数。再次,不同的是微妙的,但这基本上等同于人工添加:std::ostream& operator<<( std::ostream&, const Test<int>& )当实例Test<int>,而当你实例Testdouble另一个类似的过载,或与任何其他类型。

第三个版本是比较繁琐。如果没有内联的代码,并通过使用模板,你可以声明模板类的一个朋友的单实例化,而无需打开自己所有其它实例:

// Forward declare both templates:
template <typename T> class Test;
template <typename T> std::ostream& operator<<( std::ostream&, const Test<T>& );

// Declare the actual templates:
template <typename T>
class Test {
   friend std::ostream& operator<< <T>( std::ostream&, const Test<T>& );
};
// Implement the operator
template <typename T>
std::ostream& operator<<( std::ostream& o, const Test<T>& t ) {
   // Can only access Test<T> for the same T as is instantiating, that is:
   // if T is int, this template cannot access Test<double>, Test<char> ...
}

以性格外向的优势

这第三个选项,第一个微妙区别是,你有多少开放给其他类。滥用外向版本的一个例子是有人希望得到访问到你的内部,并做到这一点:

namespace hacker {
   struct unique {}; // Create a new unique type to avoid breaking ODR
   template <> 
   std::ostream& operator<< <unique>( std::ostream&, const Test<unique>& )
   {
      // if Test<T> is an extrovert, I can access and modify *any* Test<T>!!!
      // if Test<T> is an introvert, then I can only mess up with Test<unique> 
      // which is just not so much fun...
   }
}

15
投票

你不能声明一个这样的朋友,你需要指定不同的模板类型吧。

template <typename SclassT>
friend ostream& operator<< (ostream & os, const D<SclassT>& rhs);

注意SclassT所以它不会影classT。当定义

template <typename SclassT>
ostream& operator<< (ostream & os, const D<SclassT>& rhs)
{
  // body..
}

3
投票

这个工作对我来说没有任何编译器警告。

#include <iostream>
using namespace std;

template <class T>
T my_max(T a, T b)
{
  if(a > b)
    return a;
  else
    return b;
}

template <class classT>
class D
{
public:
  D(classT in)
    : d(in) {};

  bool operator>(const D& rhs) const {
    return (d > rhs.d);
  }

  classT operator=(const D<classT>& rhs);

  friend ostream& operator<< (ostream & os, const D& rhs) {
    os << rhs.d;
    return os;
  }

private:
  classT d;
};


int main()
{

  int i1 = 1;
  int i2 = 2;
  D<int> d1(i1);
  D<int> d2(i2);

  cout << my_max(d1,d2) << endl;
  return 0;
}

0
投票

干得好:

#include <cstdlib>
#include <iostream>
using namespace std;

template <class T>
T my_max(T a, T b)
{
   if(a > b)      
      return a;
   else
      return b;
}

template <class classT>
class D
{
public:
   D(classT in)
      : d(in) {};
   bool operator>(const D& rhs) const { return d > rhs.d;};
   classT operator=(const D<classT>& rhs);

   template<class classT> friend ostream& operator<< (ostream & os, const D<classT>& rhs);
private:
   classT d;
};

template<class classT> ostream& operator<<(ostream& os, class D<typename classT> const& rhs)
{
    os << rhs.d;
    return os;
}


int main()
{

   int i1 = 1;
   int i2 = 2;
   D<int> d1(i1);
   D<int> d2(i2);

   cout << my_max(d1,d2) << endl;
   return 0;
}

0
投票

我认为你不应该让朋友摆在首位。

您可以创建一个公共方法调用打印,这样的事情(对非模板类):

std::ostream& MyClass::print(std::ostream& os) const
{
  os << "Private One" << privateOne_ << endl;
  os << "Private Two" << privateTwo_ << endl;
  os.flush();
  return os;
}

然后,类外(但在相同的命名空间)

std::ostream& operator<<(std::ostream& os, const MyClass& myClass)
{
  return myClass.print(os);
}

我想这也应该适用于模板类,但我尚未对其进行测试。

© www.soinside.com 2019 - 2024. All rights reserved.