我收到一个不寻常的错误:
错误:“&”之前的预期不合格 ID 令牌
源代码:
// 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;
}