我得到一个不寻常的错误:我得到一个错误[错误:前 '和' 令牌预期不合格-ID]在C++程序
error: expected unqualified-id before ‘&’ token
源代码:
// Overloading the c++ array subscript operator [ ]
#include<iostream>
using namespace std;
const int size=10;
class myArray
{
int a[size];
public:
myArray()
{}
int & operator [](int);
void print_array();
};
int myArray & operator [](int x) // This is the line where error is as by compiler
{
return a[x];
}
void myArray::print_array()
{
for (int j=0; j < 10; j++)
cout<<"array["<<j<<"] = "<<a[j]<<"\n";
}
int main()
{
myArray instance;
for (int i=0; i < size; i++)
{
instance[i] = i;
}
instance.print_array();
cout<<"\n\n";
return 0;
}
您正确定义了您的'print_array'方法,这意味着您熟悉方法定义的C++语法。然而,'operator []'定义中的语法完全搞砸了,变成了无意义的标记序列。这怎么可能? – AnT 2011-04-03 06:02:41
我正在学习操作符重载的概念,并且我也尝试了以下建议的方式,但没有成功 – 2011-04-03 06:33:53