首页 > 技术文章 > VS2013 error C2556: “const int &Array<int>::operator [](int)”: 重载函数与“int &Array<int>::operator [](int)”只是在返回类型上不同

zpcoding 2019-03-06 18:03 原文

1,VS2013 

错误 1 error C2556: “const int &Array<int>::operator [](int)”: 重载函数与“int &Array<int>::operator [](int)”只是在返回类型上不同 

出错代码:

出错原因:

在 C++ 中,两个只有返回类型不同的函数不可以实现重载,重载只是参数类型不同才可以重载。想知道这种情况下如何实现重载,可点击这里

错误解决:

把第二个改为:const T& operator[] (int) const;即可

因为 const 类型的函数可以和一般函数实现重载操作。只读函数和非只读函数可以形成重载关系。

推荐阅读