apocalypse1212 apocalypse1212 - 12 days ago 6
C++ Question

Using <bits/stdc++.h> gives a global variable ambiguity

I was creating a basic tic tac toe game on c++,I got the desired game output without including bits/stdc++ header file,but when included there was an ambiguity for global variable count(which is in use in the below mentioned code). Please explain!

#include <iostream>
#include "unistd.h"
#include <cstdlib>
#include<bits/stdc++.h>
using namespace std;
char a[3][3];
int count=0;
char player_flag ='X';
void init()
{
a[0][0]='1';
a[0][1]='2';
a[0][2]='3';
a[1][0]='4';
a[1][1]='5';
a[1][2]='6';
a[2][0]='7';
a[2][1]='8';
a[2][2]='9';
}
void show()
{
for(int i=0;i<3;i++)
{ for(int j=0;j<3;j++) cout<<a[i][j] << " " ;
cout << "\n" ;
}}
void entry(int n,char player_flag)
{
for(int i=0;i<3;i++)
{ for(int j=0;j<3;j++)
{ if(n==(i*3+j+1))
{if(a[i][j]=='X'||a[i][j]=='O')
{ int n;
cout<<"invalid entry enter another position\n";
cin>>n; entry(n,player_flag);
}
else a[i][j]=player_flag;
}}}}
void turn()
{
if(player_flag=='X') player_flag='O';
else player_flag ='X';
}
void check()
{ int i,j;
for(i=0,j=0;j<3;i=0,j++)
{if(a[i][j]==a[i+1][j]&&a[i+1][j]==a[i+2][j]) {cout<<"\n"<<a[i][j]<<" wins \n"; exit(0);}}
for(i=0,j=0;i<3;j=0,i++)
{if(a[i][j]==a[i][j+1]&&a[i][j+1]==a[i][j+2]) {cout<<"\n"<<a[i][j]<<" wins \n"; exit(0);}}
if(a[0][0]==a[1][1]&&a[1][1]==a[2][2])
{cout<<"\n"<<a[0][0]<<" wins";exit(0);}
else if(a[0][2]==a[1][1]&&a[1][1]==a[2][0])
{cout<<"\n"<<a[0][2]<<" wins";exit(0);}
else if(count>=9){ cout<<"\nits a draw\n"; exit(0);}}
int main()
{ init(); show();
while(1)
{ int n; count++;
cout<<"player "<<player_flag<<" turn: enter position to put \n"; cin>>n;
entry(n,player_flag);
system("clear");
show();
check();
turn();`
}}


error: reference to ‘count’ is ambiguous
else if(count>=9){ cout<<"\nits a draw\n"; exit(0);}}

This is one of many ambiguous count errors.

PS: if bits/stdc++ is not included then its works fine,error pops out only when bits/stdc++ is used. Any reply is encouraged, Thanks!

Answer

std::count is a function from the standard library.
http://www.cplusplus.com/reference/algorithm/count/

Since you use the namespace std , "count" can refer to either std::count or the variable count.

You need to either rename your variable, or stop using the std namespace.
You can also include only the c++ headers that you need instead of bits/stdc++.h which includes all of them.

Comments