error: no match for ‘operator[]’

hi everyone,
i have written a code in c++. first i created a structure "elements". then declared a array "m" of datatype "elements" in the main(). When i passed this variable to a function by reference, i got this error

tdma.cpp:7:12: error: no match for ‘operator[]’

i got the same error for many lines within the function
below is the code which i am working on.

#include<iostream>
struct elements
{ float a,b,d,p,f,q;
};
int tdma(struct elements m)
{ int n=0;
while(m[n].a!='\0') // here is the error- m[n].a
{ n++;}
m[0].p=-(m[0].b)/(m[0].d);
m[0].q=m[0].f/m[0].d;
for(int i=1;i<n;i++)
{ m[i].p=-m[i].b/(m[i].d+m[i].a*m[i-1].p);
m[i].q=(m[i].f-m[i].a*m[i-1].q)/(m[i].d+m[i].a*m[i-1].p);
}
m[n-1].p=0;
return 0;
};
int main()
{ using namespace std;
float tdm[4][4]; elements m[4];
int n=4;

for(int i=0;i<n;i++)
{ tdm[i][i]=-2;
for(int j=1;j<n;j++)
{ if((i-j)*(i-j)==1)
tdm[i][j]==1;
}
}
m[0].a=2;
cout<<m[0].a;
return 0;
}

please tell me what's going wrong. thanks in advance

int tdma(struct elements m)
Inside the function tdma, m is not an array. It is a single elements object.

If you want to pass in an array, try
int tdma(elements* m)

I see that in your code you never actually call the function tdma. If you aren't using it, you can just delete it.
forgot to tell you, the program is not complete. there may be other irrelevant mistakes in the program, please dont care about that and just help me with the error.
thanks Moschops for the help, i dont see the error now after the modification you suggested. just one more thing, if i want to pass it by reference then can i try

int tdma(elements* &m)
closed account (1vRz3TCk)
1
2
int tdma(elements (&m)[4])
{...}
Here, m is a reference to an array of 4 elements
Last edited on
You can pass the array by reference, but there's very little point in doing that. When you pass an array, you actually pass a pointer to that array, so any changes you make persist when the function returns.
closed account (1vRz3TCk)
When you pass a pointer to an array you would also need to pass the dimensions of the array.
thanks for the help
When you pass a pointer to an array you would also need to pass the dimensions of the array.


I disagree. I regularly pass just a pointer to an array. It's common practice. strcpy springs to mind as a commonly used function in which just a pointer to the array is passed.
Last edited on
closed account (1vRz3TCk)
Moschops wrote:
I regularly pass just a pointer to an array. It's common practice.
I never said otherwise. I also regularly pass pointer to arrays (along with the dimensions of the array). Char arrays are a bit different because they use a null character to terminate the string but on the whole you need a method of knowing how big the array is.
Topic archived. No new replies allowed.