相关疑难解决方法(0)

重载friend operator <<用于模板类

我现在已经在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> …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading friend ostream

56
推荐指数
2
解决办法
4万
查看次数

C++:friend declaration'声明一个非模板函数

我有一个问题是重载<<流操作符,我找不到解决方案:

template<class T, unsigned int TN>
class NVector
{
    inline friend std::ostream& operator<< (
        std::ostream &lhs, const NVector<T, TN> &rhs);
};

template<class T, unsigned int TN>
inline std::ostream& NVector<T, TN>::operator<<(
    std::ostream &lhs, const NVector<T, TN> &rhs)
{
    /* SOMETHING */
    return lhs;
};
Run Code Online (Sandbox Code Playgroud)

它会产生以下错误消息:

警告:朋友声明'std :: ostream&operator <<(std :: ostream&,const NVector&)'声明一个非模板函数[-Wnon-template-friend]

错误:'std :: ostream&NVector :: operator <<(std :: ostream&,const NVector&)'必须只有一个参数

如何解决这个问题?

非常感谢你.

c++ gcc templates ostream

5
推荐指数
1
解决办法
7809
查看次数

标签 统计

c++ ×2

ostream ×2

templates ×2

friend ×1

gcc ×1

operator-overloading ×1