C ++朋友模板函数:找不到函数定义

时间:2020-08-21 21:23:06

标签: c++ templates friend

在尝试围绕C ++模板时,我遇到了一个编译器警告,我不明白。在vec.h的下面,Visual Studio 2019将在operator<<<T>的{​​{1}}下的绿色花体中显示警告“找不到class vec的函数定义。

否则,代码将编译并正常运行。特别是,我不了解这种情况与不会产生警告的operator<<<T>有何不同。

operator+

#pragma once #include <iostream> template<typename T> class vec; template<typename T> vec<T> operator+(const vec<T>& a, const vec<T>& b); template<typename T> std::ostream& operator<<(std::ostream& os, const vec<T>& v); template<typename T> class vec { public: vec(T xx = 0, T yy = 0) : x(xx), y(yy) {} friend vec<T> operator+<T>(const vec<T>& a, const vec<T>& b); friend std::ostream& operator<<<T>(std::ostream& os, const vec<T>& v); private: T x, y; }; 中,我为朋友函数和一些显式实例化放置了函数模板。

vec.cpp

1 个答案:

答案 0 :(得分:0)

这只是Visual Studio IntelliSense的不正确警告。该代码格式正确并已成功编译。

相关问题