我正在尝试创建一个模板类,其中一个人可以将类使用的(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<<'
我可能做错了什么,但我无法理解。
感谢您的帮助, ADAM
P.S。引用的类具有以下定义:
// 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。
答案 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;
答案 1 :(得分:1)
根据GCC 4.3和4.4编译,但4.1给出的错误与您描述的类似。必须是GCC的错误。