c++ - 错误:将“操作员*”声明为非功能
问题描述
我对模板类的绑定模板友元函数有一些问题。我参考了与我有类似和简单问题的页面:vect.hpp:13:33: error: declaration of 'operator<<' as non-function ,但我仍然感到困惑,问题无法解决。这是我的代码:
#include<iostream>
#include<valarray>
using namespace std;
//forward declaration
template<typename ElemType, size_t row, size_t col = row>
class matrix;
template<typename ET, size_t R, size_t C>
matrix<ET, R, C> operator*(ET multier, matrix<ET, R, C> mattem);
template<typename ElemType, size_t row, size_t col>
class matrix
{
private:
valarray<ElemType> mat;
public:
matrix(): mat(row*col) {}
matrix(initializer_list<ElemType>);
matrix operator*(ElemType multier) const
{
matrix mattem;
mattem.mat = this->mat*multier;
return mattem;
}
//friend function prototype
friend matrix<ElemType, row, col> operator*<>(ElemType multier, matrix<ElemType, row, col> mattem);
void show() const
{
for (ElemType Elem : this->mat)
cout << Elem << " ";
cout << endl;
}
};
template<typename ElemType, size_t row, size_t col>
matrix<ElemType, row, col>::matrix(initializer_list<ElemType> ini)
{
try
{
if (ini.size() != row*col)
throw "Inappropriate length!";
this->mat = ini;
}
catch (const char * str)
{
cout << str << endl;
}
}
//friend function definition
template<typename ElemType, size_t row, size_t col>
matrix<ElemType, row, col> operator*<>(ElemType multier, matrix<ElemType, row, col> mattem)
{
return mattem*multier;
}
int main(void)
{
matrix<int, 2, 3> test = {1, 3, 5, 8, 9, 7};
matrix<int, 2, 3> abc =2*test;
abc.show();
}
WSL2 (Ubuntu 9.3.0-17ubuntu1~20.04)
我在using中编译并运行它gcc version 9.3.0
,我得到了一系列错误,如下所示:
test.cpp:28:51: error: declaration of ‘operator*’ as non-function
28 | friend matrix<ElemType, row, col> operator*<>(ElemType multier, matrix<ElemType, row, col> mattem);
| ^
test.cpp:28:51: error: expected ‘;’ at end of member declaration
28 | friend matrix<ElemType, row, col> operator*<>(ElemType multier, matrix<ElemType, row, col> mattem);
| ^
| ;
test.cpp:28:52: error: expected unqualified-id before ‘<’ token
28 | friend matrix<ElemType, row, col> operator*<>(ElemType multier, matrix<ElemType, row, col> mattem);
| ^
test.cpp:54:91: error: template-id ‘operator*<>’ in declaration of primary template
54 | matrix<ElemType, row, col> operator*<>(ElemType multier, matrix<ElemType, row, col> mattem)
| ^
test.cpp: In function ‘int main()’:
test.cpp:62:29: error: conversion from ‘matrix<[...],[...],2>’ to non-scalar type ‘matrix<[...],[...],3>’ requested
62 | matrix<int, 2, 3> abc =2*test;
| ~^~~~~
解决方案
我发现在模板类中处理友元运算符最可靠的方法是将完整的定义放在类本身中:
//forward declaration
template<typename ElemType, size_t row, size_t col = row>
class matrix;
/*
template<typename ET, size_t R, size_t C>
matrix<ET, R, C> operator*(ET multier, matrix<ET, R, C> mattem);
*/
template<typename ElemType, size_t row, size_t col>
class matrix
...
//friend function prototype
friend matrix<ElemType, row, col> operator * (ElemType multier, matrix<ElemType, row, col> mattem){
return mattem * multier;
}
...
/*
//friend function definition
template<typename ElemType, size_t row, size_t col>
matrix<ElemType, row, col> operator*(ElemType multier, matrix<ElemType, row, col> mattem)
{
return mattem*multier;
}
*/
推荐阅读
- replication - 使用 Firebird Trace 和 Audit 服务进行数据库复制
- reactjs - React 更改一个子组件中的状态并将其传递给另一个子组件
- google-cloud-run - 有没有办法将 IP 地址列入白名单来访问 Cloud Run 服务?
- hadoop - 我在猪中使用过滤器时遇到错误,当我转储结果时它会出错
- phpstorm - 如何在 PhpStorm 中的变量和运算符之间留出空间?
- swift - 如何从另一个视图控制器中删除表格视图中的单元格?
- python - 如何在 df1 的 c1 列中分别合并两个数据帧 df1 和 df2 的两列 c1 和 c2 ?c1 和 c2 是字典
- python - 需要从函数返回的列表的多处理 Pandas 数据帧输出
- django - url 调度程序如何包含应用程序 url
- flutter - 如何使 Wrap 尽可能不最小化高度(减少行数)