使用模板模板参数'operator<<'的不明确重载

Ambiguous overload for 'operator<<' with template template arguments

本文关键字:lt 不明确 重载 operator 参数      更新时间:2023-10-16

我试图使一个模板类,其中一个将能够定义(STL)容器使用的类作为模板参数。所以我做了如下操作:

template <typename T = float,
          template < class, class > class Container = std::vector,
          class Alloc = typename std::vector < Spectrum < T > >::allocator_type>
class Spectrogram;
我的问题是,当我将流操作符的重载定义为 时
// Definition inside the 'public' part of the 'Spectrogram' class
template <typename Type,
          template < class, class > class C,
          class A>
friend std::ostream & operator << ( std::ostream &, const Spectrogram < Type, C, A > & );
// Implementation
template <typename Type,
          template < class, class > class C,
          class A>
std::ostream & operator << ( std::ostream & o, const Spectrogram < Type, C, A > & s )
{
   // Transparent implementation
   return o;
}
然后创建一些测试代码作为
Spectrogram < float > spectrogram;
// Fill the spectrogram etc. -- codepart omitted
std::cout << spectrogram;

我得到以下错误:

Ambiguous overload for 'operator<<'

我可能做错了什么事,但我就是想不出来。

谢谢你的帮助,亚当

注:所引用的类具有以下定义:

// Spectrum
template < typename T = float, class Container = std::vector < T > > class Spectrum {
public:
   typedef T ( * WindowFunction ) ( const T & );
   typename Container::const_iterator begin ( void ) const;
   typename Container::const_iterator end ( void ) const;
   typename Container::const_reverse_iterator rbegin ( void ) const;
   typename Container::const_reverse_iterator rend ( void ) const;
   WindowFunction getWindowFunction ( void ) const;
   typename Container::size_type getWindowSize ( void ) const;
   typename Container::size_type getFFTSize ( void ) const;
   void setWindowFunction ( const WindowFunction & );
   void setFFTSize ( const typename Container::size_type & );
   template < class InputIterator > void import ( const InputIterator &, const InputIterator & );
   template < typename Type, class C > friend std::ostream & operator << ( std::ostream &, const Spectrum < Type, C > & );
protected:
   WindowFunction windowFunction;
   typename Container::size_type windowSize;
   Container spectrum;
   void clear ( void );
};
// Spectrogram
template < typename T = float, template < class, class > class Container = std::vector, class Alloc = typename std::vector < Spectrum < T > >::allocator_type > class Spectrogram {
public:
   typedef typename Container < Spectrum < T >, Alloc >::const_iterator Iterator;
   typedef typename Container < Spectrum < T >, Alloc >::const_reverse_iterator ReverseIterator;
   typedef typename Container < Spectrum < T >, Alloc >::size_type size_type;
   Iterator begin ( void ) const;
   Iterator end ( void ) const;
   ReverseIterator rbegin ( void ) const;
   ReverseIterator rend ( void ) const;
   size_type size ( void ) const;
   bool empty ( void ) const;
   WindowTypes getWindowType ( void ) const;
   double getHopFactor ( void ) const;
   unsigned long getWindowSize ( void ) const;
   unsigned short getOversamplingFactor ( void ) const;
   unsigned long getHopSize ( void ) const;
   void setWindowType ( const WindowTypes & );
   void setHopFactor ( const double & );
   void setWindowSize ( const unsigned long & );
   void setOversamplingFactor ( const unsigned short & );
   template < class InputIterator > void import ( const InputIterator &, const InputIterator & );
   template < typename Type, template < class, class > class C, class A > friend std::ostream & operator << ( std::ostream &, const Spectrogram < Type, C, A > & );
protected:
   Container < Spectrum < T >, Alloc > spectrogram;
   double hopFactor;
   unsigned long windowSize;
   unsigned short oversamplingFactor;
   WindowTypes windowType;
};

我使用GCC 4.2和XCode 4.0.2.

虽然这个问题似乎可以通过编译器升级或轻微的语法调整来解决,但您可能希望简化参数化。

class Spectrogram实际上只取决于用于包含其数据的类。因此,这将在不损失功能的情况下工作:

template< typename Container >
class Spectrum {
public:
    typedef typename Container::value_type value_type;
    ...
};
template< typename Container >
class Spectrogram {
    // assume Container::value_type is a specialization of Spectrum
    typedef typename Container::value_type::value_type numeric_type;
};

这消除了默认参数的级联序列,但大大简化了设计。您可以通过提供typedef和(如果您确实使用各种参数化)将所需参数映射到所需Container的元函数来再次简化使用。

typedef Spectrum< vector< float > > Spectrum_sp; // single-precision
typedef Spectrograph< Spectrum_sp > Spectrograph_sp;

在GCC 4.3和4.4下编译,但4.1给出了与您描述的类似的错误。