Muhammad Faqih Muhammad Faqih - 2 years ago 151
C++ Question

How to fix this Error expression must have a class type and C228?

I'm new in C++. I'm trying to write sample code from my Java Book to practice C++.

Here the source code :

#include <iostream>
using namespace std;

void tampilkanMatriks(int matriks[3][2]);
void tampilkanMatriks(int matriks[3][2])
int jumBaris = matriks.length;
int jumKolom = matriks[0].length;

for (int i = 0; i < jumBaris; i++)
for (int j = 0; j < jumKolom; j++)
printf("%4d", matriks[i][j]);

cout << endl;

int main()
int mat[3][2];

mat[0][0] = 70;
mat[0][1] = 80;

mat[1][0] = 64;
mat[1][1] = 55;

mat[2][0] = 76;
mat[2][1] = 72;


return 0;

Error : expression must have a class type in line 7, expression must have a class type in line 8, C2228 error in line 7 and 8.

Answer Source

Arrays are not class objects. matriks.length is invalid. The . operator is used for class and struct. Since you have hard typed the dimensions of the array you can always use them to iterate through the array. Although this is not a good solution. In general, when you are dealing with arrays its best to pass an extra argument which corresponds to the size of the array. If you need to use a class you havestd::vector.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download