使用<bits stdc ++。h =“”>给出全局变量歧义

时间:2016-11-23 17:37:02

标签: c++ stl

我在c ++上创建了一个基本的tic tac toe游戏,我得到了所需的游戏输出而没有包含bits / stdc ++头文件,但是当包含时,全局变量计数存在歧义(在下面提到的代码中使用了它) )。请解释!

#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();`
}}

错误:对'count'的引用不明确     否则if(count&gt; = 9){cout&lt;&lt;“\ nits a draw \ n”;出口(0);}}

这是许多模棱两可的计数错误之一。

PS:如果不包含bits / stdc ++,那么它的工作正常,只有在使用bits / stdc ++时才会弹出错误。鼓励任何回复,谢谢!

3 个答案:

答案 0 :(得分:2)

我怀疑count位于std命名空间中。

删除行

using namespace std;

在任何需要显式的地方使用命名空间说明符std::

你不应该使用

#include<bits/stdc++.h>

反正。使用属于标准的标题。

<强> PS

来自the answer by @CFrugal

std::count是标准库中的函数 http://www.cplusplus.com/reference/algorithm/count/

答案 1 :(得分:2)

std::count是标准库中的函数 http://www.cplusplus.com/reference/algorithm/count/

由于您使用命名空间std,“count”可以引用std::count或变量count

您需要重命名变量,或者停止使用std命名空间 您还可以只包含所需的c ++头而不是bits / stdc ++。h,其中包含所有这些头文件。

答案 2 :(得分:0)

bits/目录中的文件是实现详细信息,不会直接包含在您的程序中。它们由<vector><iostream>等常规内容间接包含在内。由于它是一个实现细节,因此允许对其所包含的上下文进行假设,并且可能是您的包含位置违反了其中一个假设。

只需包含所需功能的常规标准标头,而不是bits文件。

第二次看到你的问题后,你可能还会遇到第二个问题:using namespace stdstd::count函数带入全局命名空间,该命名空间与全局int count发生冲突。要解决此问题,请考虑使用标准的特定函数而不是整个命名空间(using std::cout;),或重命名计数变量,或者不要在全局范围内声明它。

相关问题