-1

The following code is giving this error:

error: no match for 'operator[]' (operand types are 'S' and 'int')|"

in the lines 38, 40, 52, 53, 54, 54, 67, 67 (Yes, same error at at 54 and 67 two times).

#include <iostream>

using namespace std;

const int m=3;

class S
{
public:

    int col;
    char ch;


    int getcolumn()
    {
        return col;
    }

    int getrow()
    {
        int t=ch; t-=65;
        return t;
    }
};


void input(S *mat)
{
    int i,j;

    for(i=0;i<m;i++)
    {
        cout<<endl<<"ROW "<<i+1;
        for(j=0;j<m;j++)
        {
            cout<<endl<<"COLUMN "<<j+1<<endl;
            cout<<"enter the number";
            cin>>mat[i][j].col;
            cout<<"enter the letter";
            cin>>mat[i][j].ch;
        }
    }
}

void logic(S *orig,S *repl)
{
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<m;j++)
        {
            int coll=orig[i][j].getcolumn();
            int row=orig[i][j].getrow();
            repl[row][coll]=orig[i][j];
        }
    }
}


void output(S *repl)
{
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<m;j++)
        {
            cout<<repl[i][j].col<<repl[i][j].ch<<"   ";
        }
        cout<<endl;
    }
}

int main()
{
    int i,j;

    S orig[10][10],repl[10][10];

    input(&orig[0][0]);
    logic(&orig[0][0],&repl[0][0]);
    output(&repl[0][0]);

    return 0;
}

How can the error be solved? I am using code::blocks 17.12 with GCC compiler. I am quite new to c++ so please explain in a little bit more detailed manner.

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
Maverick
  • 29
  • 4
  • 1
    `S` doesn't have any idea what `operator[]` is supposed to do. See https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading – François Andrieux May 25 '18 at 14:02
  • 2
    Welcome to StackOverflow. You should start with [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). I think this program is not really doing what you expect it to do (even if you solve the operator problem). – Yksisarvinen May 25 '18 at 14:04
  • The basic problem is that you are passing a pointer to the first element of the first array in `orig` (and `repl`) and then try to treat it as if it was the array. But it's not an array, it's a pointer to the first element. – Max Langhof May 25 '18 at 14:08
  • Just use `std::array` or `std::vector` – Slava May 25 '18 at 14:09

1 Answers1

1

The problem is that once an array is converted to a pointer, you can only do 1D pointer arithmetic on it. Your functions take parameters of type S *, that is, a pointer to S.

Let's take output(S *repl). repl is a pointer to an S. This means that repl[i] is (a reference to) an S. Now, you apply [j] to that again, but S doesn't have any such operator, and so it errors out.

What you need to do is do the 2D indexing manually, perhaps like this:

size_t idx2d(size_t i, size_t j, size_t iCount)
{
    return j * iCount + i;
}

void output(S *repl, size_t iCount)
{
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<m;j++)
        {
            cout<<repl[idx2d(i, j, iCount)].col<<repl[idx2d(i, j, iCount)].ch<<"   ";
        }
        cout<<endl;
    }
}

// In main:

output(&repl[0][0], 10);
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455