“中的错误”;C++;程序设计语言;第「;23.4.7“朋友”; 我一直试图从《C++程序设计语言》(第四版)部分“23.4.7朋友”中获得以下代码,但不能成功。 template<typename T> class Matrix; template<typename T> class Vector { T v[4]; public: friend Vector operator*<>(const Matrix<T>&, const Vector&); /* line 7 */ // ... }; template<typename T> class Matrix { Vector<T> v[4]; public: friend Vector<T> operator*<>(const Matrix&, const Vector<T>&); /* line 14 */ // ... }; template<typename T> Vector<T> operator*(const Matrix<T>& m, const Vector<T>& v) { Vector<T> r; // ... m.v[i] and v.v[i] for direct access to elements ... return r; }
给出了以下输出:“中的错误”;C++;程序设计语言;第「;23.4.7“朋友”; 我一直试图从《C++程序设计语言》(第四版)部分“23.4.7朋友”中获得以下代码,但不能成功。 template<typename T> class Matrix; template<typename T> class Vector { T v[4]; public: friend Vector operator*<>(const Matrix<T>&, const Vector&); /* line 7 */ // ... }; template<typename T> class Matrix { Vector<T> v[4]; public: friend Vector<T> operator*<>(const Matrix&, const Vector<T>&); /* line 14 */ // ... }; template<typename T> Vector<T> operator*(const Matrix<T>& m, const Vector<T>& v) { Vector<T> r; // ... m.v[i] and v.v[i] for direct access to elements ... return r; },c++,templates,operator-overloading,friend,C++,Templates,Operator Overloading,Friend,给出了以下输出: test.cpp:7: error: declaration of ‘operator*’ as non-function test.cpp:7: error: expected ‘;’ before ‘<’ token test.cpp:14: error: declaration of ‘operator*’ as non-function test.cpp:14: error: expected ‘;’ before ‘<’ token test2.cpp:
test.cpp:7: error: declaration of ‘operator*’ as non-function
test.cpp:7: error: expected ‘;’ before ‘<’ token
test.cpp:14: error: declaration of ‘operator*’ as non-function
test.cpp:14: error: expected ‘;’ before ‘<’ token
test2.cpp:7:19: error: friends can only be classes or functions
friend Vector operator*<>(const Matrix<T>&, const Vector&);
^
test2.cpp:7:28: error: expected ';' at end of declaration list
friend Vector operator*<>(const Matrix<T>&, const Vector&);
^
;
test2.cpp:14:22: error: friends can only be classes or functions
friend Vector<T> operator*<>(const Matrix&, const Vector<T>&);
^
test2.cpp:14:31: error: expected ';' at end of declaration list
friend Vector<T> operator*<>(const Matrix&, const Vector<T>&);
^
;
4 errors generated.
给出了以下输出:
test.cpp:7: error: declaration of ‘operator*’ as non-function
test.cpp:7: error: expected ‘;’ before ‘<’ token
test.cpp:14: error: declaration of ‘operator*’ as non-function
test.cpp:14: error: expected ‘;’ before ‘<’ token
test2.cpp:7:19: error: friends can only be classes or functions
friend Vector operator*<>(const Matrix<T>&, const Vector&);
^
test2.cpp:7:28: error: expected ';' at end of declaration list
friend Vector operator*<>(const Matrix<T>&, const Vector&);
^
;
test2.cpp:14:22: error: friends can only be classes or functions
friend Vector<T> operator*<>(const Matrix&, const Vector<T>&);
^
test2.cpp:14:31: error: expected ';' at end of declaration list
friend Vector<T> operator*<>(const Matrix&, const Vector<T>&);
^
;
4 errors generated.
test2.cpp:7:19:错误:好友只能是类或函数
友元向量算子*(常数矩阵&,常数向量&);
^
test2.cpp:7:28:错误:应为“;”在申报表末尾
友元向量算子*(常数矩阵&,常数向量&);
^
;
test2.cpp:14:22:错误:朋友只能是类或函数
友元向量算子*(常数矩阵&,常数向量&);
^
test2.cpp:14:31:错误:应为“;”在申报表末尾
友元向量算子*(常数矩阵&,常数向量&);
^
;
产生了4个错误。
书上说:
需要在firend函数的名称后面加上
朋友是一个模板函数
我无法在代码中发现问题,可以吗?是的,这是书中的一个错误 否则,此时函数模板必须已可见 因此,将操作符定义移到
Vector
的定义之上(这本身要求我们提供一个向前声明)
test2.cpp:7:19: error: friends can only be classes or functions
friend Vector operator*<>(const Matrix<T>&, const Vector&);
^
test2.cpp:7:28: error: expected ';' at end of declaration list
friend Vector operator*<>(const Matrix<T>&, const Vector&);
^
;
test2.cpp:14:22: error: friends can only be classes or functions
friend Vector<T> operator*<>(const Matrix&, const Vector<T>&);
^
test2.cpp:14:31: error: expected ';' at end of declaration list
friend Vector<T> operator*<>(const Matrix&, const Vector<T>&);
^
;
4 errors generated.