0

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!

3 Answers3

4

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.

C. Frugal
  • 112
  • 4
2

I suspect count is in std namespace somewhere.

Remove the line

using namespace std;

Use the namespace specifier std:: wherever you need it explicitly.

You should not use

#include<bits/stdc++.h>

anyway. Use headers that are part of the standard.

PS

From the answer by @CFrugal:

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

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

The files in the bits/ directory are implementation details not to be included in your programs directly. They are included indirectly by the normal includes like <vector> and <iostream>. Since it's an implementation detail, it's allow to make assumptions about the context in which it's included and presumably your include location violates one of those assumptions.

Just include the normal standard header for the functionality you need instead of a bits file.

Upon reading your question a second time it looks like you may also have a second problem: using namespace std bringing the std::count function into the global namespace, which collides with your global int count. To fix this consider using specific functions from standard instead of the entire namespace (using std::cout;), or rename your count variable, or don't declare it at global scope.

Mark B
  • 95,107
  • 10
  • 109
  • 188