C ++ []数组运算符多个参数? [英] C++ [] array operator with multiple arguments?

查看:152
本文介绍了C ++ []数组运算符多个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否定义在C ++中数组运算符需要多个参数?我想这样的:

 常量T&安培;运算符[](const int的我,常量诠释J,const int的K)常量{
返回m_cells [K * m_resSqr + J * m_res + 1];
}夯;运算符[](const int的我,常量诠释J,const int的K){
返回m_cells [K * m_resSqr + J * m_res + 1];
}

但我发现了这个错误:

 错误C2804二元运算符'['有太多的参数


解决方案

不,你不能重载运算符[] 来接受多个参数。你不是可以重载操作符()。见如何创建一个下标运算符的Matrix类?从C ++ FAQ。

Can I define in C++ an array operator that takes multiple arguments? I tried it like this:

const T& operator[](const int i, const int j, const int k) const{ 
	return m_cells[k*m_resSqr+j*m_res+i];
}

T& operator[](const int i, const int j, const int k){ 
	return m_cells[k*m_resSqr+j*m_res+i];		
}

But I'm getting this error:

error C2804 binary operator '[' has too many parameters

解决方案

Nope, you can't overload operator[] to accept multiple arguments. You instead can overload operator(). See How do I create a subscript operator for a Matrix class? from the C++ FAQ.

这篇关于C ++ []数组运算符多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆