Expression must have class type error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>

char grid[7][6];
int main(){
	char grid[7][6];
	int i;
	int j;
	for (i = 0; i < 7; i++){
		for (j = 0; j < 6; j++){
			grid[i][j] = 'O';
			std::cout << grid[i][j];
		}
		std::cout << std::endl;
	}
	int hor;
	int ver;
	std::cin >> hor;
	std::cin >> ver;
	std::replace(grid[hor][ver].begin(), grid[hor][ver].end(), 'O', 'P'); //error here
}
Last edited on
Maybe it's because of the double definition of grid[7][6].
1
2
3
char grid[7][6];
int main(){
	char grid[7][6];

Remove the one outside main, variables are usually included in it.
Last edited on
Did that. The error still persists :/
Could you copy and paste the error message exactly?
Line 22 doesn't make sense. What are you trying to do there?

Something like this: grid[hor][ver] = 'P';?
char grid[7][6]
Error: expression must have class type
Error 1 error C2228: left of '.begin' must have class/struct/union
Error 2 error C2228: left of '.end' must have class/struct/union
Error 3 error C2780: 'void std::replace(_FwdIt,_FwdIt,const _Ty &,const _Ty &)' : expects 4 arguments - 3 provided
4 IntelliSense: expression must have class type
5 IntelliSense: expression must have class type
Did a bit of research on replace();
It seems that you need to use a vector for the replace function.
You could split the array in colums and store them in vectors, but that would be a total mess and it's already giving me a headashe.

I think the easiest thing to do is just loop trough them or write a function to do that:
1
2
3
4
5
6
for(int i=0;i<hor;i++)
for(int j=0;j<ver;i++)
{
    if(grid[i][j] == 'O')
    grid[i][j] = 'P';
}
Last edited on
Yes, 'coder777' , that's what I tried to do , but the problem is that I am actually trying to make a connect 4 game, and every time it returns the P back to O again, so I'm trying to use replace instead so every time it outputs , the last time ver and hor were written doesn't get lost, I'm sorry if I am not clear, I hope you get what I mean.
every time it returns the P back to O again

I suggest you separate out different parts of your code into individual functions.
For example in the above code:
11
12
13
14
15
16
17
    for (i = 0; i < 7; i++){
        for (j = 0; j < 6; j++){
            grid[i][j] = 'O';
            std::cout << grid[i][j];
        }
        std::cout << std::endl;
    }

There are two separate and unrelated activities going on.

1. at line 13 the initial value of the grid is being set.
2. at line 14 the contents of the grid are displayed.

If you place these unrelated activities in their own separate functions, the code becomes easier to understand, as well as giving better control.

I already gave an example of how this might be done:
http://www.cplusplus.com/forum/beginner/186659/#msg909407

Topic archived. No new replies allowed.