使用模板模板参数'operator<<'的不明确重载
Ambiguous overload for 'operator<<' with template template arguments
我试图使一个模板类,其中一个将能够定义(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给出了与您描述的类似的错误。
相关文章:
- Visual C++(VS2017)中用户定义的转换不明确
- 重载类方法的不明确调用
- 为函数定义符号不明确的指针参数
- 父类的私有函数会导致对具有相同名称和相似参数的子类中的公共函数的不明确调用
- 在 C++17 中的命名空间和子命名空间中重载运算符是不明确的
- C++ 编译器错误:P1LinkedList.cpp:145:错误:重载的"to_string(int&)"调用不明确
- 对重载函数find_first_not_of的不明确调用
- 不明确的成员模板查找
- gcc出现不明确的模板实例化错误
- 调用'Node'构造函数是不明确的
- 如何解决不明确的运算符过载问题?
- 使用 nullptr 调用重载方法是不明确的
- "fpclassify":对重载函数的不明确调用
- 对"列表"的引用不明确,包括头文件
- 删除全局隐式函数 - 避免使用不明确的运算符
- 当接收到不明确的规范时,表示图的邻接列表的数据结构
- "byte":使用Crypto++和Windows SDK时出现不明确的符号错误
- 使用 std::iterator_traits<> 时编译时错误不明确
- 不明确的函数模板
- Vulkan中的描述符集计数不明确