【 c++ 】y1,y0变量名冲突问题

源代码

#include
using namespace std;
#define N 250
int q,n,m,x1,x2,y1,y2,sum=0;
int ans[N][N];
int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);cout.tie(0);
	cin>>n>>m>>q;
	while(q--){
		cin>>x1>>y1>>x2>>y2;
		for(int i=x1;i<=x2;++i)
			for(int j=y1;j<=y2;++j)
				ans[i][j]=1;
	}
	for(int i=0;i<=n;++i)
		for(int j=0;j<=m;++j)
			if(ans[i][j]==1)
				++sum;
	cout<

编译信息

/tmp/compiler_gvh2qurl/src:4:17: 错误:‘int y1’ redeclared as different kind of entity
    4 | int q,n,m,x1,x2,y1,y2,sum=0;
      |                 ^~
In file included from /nix/store/7rfaw11na5ajdgwr55ffzwfibbrdpk8z-glibc-2.33-56-dev/include/features.h:473,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/os_defines.h:39,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/c++config.h:524,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/cassert:43,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h:33,
                 from /tmp/compiler_gvh2qurl/src:1:
/nix/store/7rfaw11na5ajdgwr55ffzwfibbrdpk8z-glibc-2.33-56-dev/include/bits/mathcalls.h:224:1: 附注:previous declaration ‘double y1(double)’
  224 | __MATHCALL (y1,, (_Mdouble_));
      | ^~~~~~~~~~
/tmp/compiler_gvh2qurl/src: 在函数‘int main()’中:
/tmp/compiler_gvh2qurl/src:11:10: 错误:no match for ‘operator>>’ (operand types are ‘std::basic_istream::__istream_type’ {aka ‘std::basic_istream’} and ‘double(double) noexcept’ {aka ‘double(double)’})
   11 |   cin>>x1>>y1>>x2>>y2;
      |   ~~~~~~~^~~~
      |      |     |
      |      |     double(double) noexcept {aka double(double)}
      |      std::basic_istream::__istream_type {aka std::basic_istream}
In file included from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/sstream:38,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/complex:45,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/ccomplex:39,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h:54,
                 from /tmp/compiler_gvh2qurl/src:1:
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/istream:120:7: 附注:candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__istream_type& (*)(std::basic_istream<_CharT, _Traits>::__istream_type&)) [with _CharT = char; _Traits = std::char_traits; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream]’ 
  120 |       operator>>(__istream_type& (*__pf)(__istream_type&))
      |       ^~~~~~~~
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/istream:120:7: 附注:  conversion of argument 1 would be ill-formed:
/tmp/compiler_gvh2qurl/src:11:12: 错误:invalid conversion from ‘double (*)(double) noexcept’ {aka ‘double (*)(double)’} to ‘std::basic_istream::__istream_type& (*)(std::basic_istream::__istream_type&)’ {aka ‘std::basic_istream& (*)(std::basic_istream&)’} [-fpermissive]
   11 |   cin>>x1>>y1>>x2>>y2;
      |            ^~
      |            |
      |            double (*)(double) noexcept {aka double (*)(double)}
In file included from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/sstream:38,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/complex:45,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/ccomplex:39,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h:54,
                 from /tmp/compiler_gvh2qurl/src:1:
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/istream:124:7: 附注:candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::basic_istream<_CharT, _Traits>::__ios_type& (*)(std::basic_istream<_CharT, _Traits>::__ios_type&)) [with _CharT = char; _Traits = std::char_traits; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream; std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios]’ 
  124 |       operator>>(__ios_type& (*__pf)(__ios_type&))
      |       ^~~~~~~~
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/istream:124:7: 附注:  conversion of argument 1 would be ill-formed:
/tmp/compiler_gvh2qurl/src:11:12: 错误:invalid conversion from ‘double (*)(double) noexcept’ {aka ‘double (*)(double)’} to ‘std::basic_istream::__ios_type& (*)(std::basic_istream::__ios_type&)’ {aka ‘std::basic_ios& (*)(std::basic_ios&)’} [-fpermissive]
   11 |   cin>>x1>>y1>>x2>>y2;
      |            ^~
      |            |
      |            double (*)(double) noexcept {aka double (*)(double)}
In file included from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/sstream:38,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/complex:45,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/ccomplex:39,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h:54,
                 from /tmp/compiler_gvh2qurl/src:1:
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/istream:131:7: 附注:candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(std::ios_base& (*)(std::ios_base&)) [with _CharT = char; _Traits = std::char_traits; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream]’ 
  131 |       operator>>(ios_base& (*__pf)(ios_base&))
      |       ^~~~~~~~
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/istream:131:7: 附注:  conversion of argument 1 would be ill-formed:
/tmp/compiler_gvh2qurl/src:11:12: 错误:invalid conversion from ‘double (*)(double) noexcept’ {aka ‘double (*)(double)’} to ‘std::ios_base& (*)(std::ios_base&)’ [-fpermissive]
   11 |   cin>>x1>>y1>>x2>>y2;
      |            ^~
      |            |
      |            double (*)(double) noexcept {aka double (*)(double)}
In file included from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/sstream:38,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/complex:45,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/ccomplex:39,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h:54,
                 from /tmp/compiler_gvh2qurl/src:1:
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/istream:168:7: 附注:candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(bool&) [with _CharT = char; _Traits = std::char_traits; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream]’ 
  168 |       operator>>(bool& __n)
      |       ^~~~~~~~
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/istream:168:7: 附注:  conversion of argument 1 would be ill-formed:
/tmp/compiler_gvh2qurl/src:11:12: 警告:‘double y1(double)’的地址永远不会为 NULL [-Waddress]
   11 |   cin>>x1>>y1>>x2>>y2;
      |            ^~
/tmp/compiler_gvh2qurl/src:11:12: 错误:cannot bind non-const lvalue reference of type ‘bool&’ to an rvalue of type ‘bool’
In file included from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/sstream:38,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/complex:45,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/ccomplex:39,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h:54,
                 from /tmp/compiler_gvh2qurl/src:1:
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/istream:172:7: 附注:candidate: ‘std::basic_istream<_CharT, _Traits>& std::basic_istream<_CharT, _Traits>::operator>>(short int&) [with _CharT = char; _Traits = std::char_traits]’ 
  172 |       operator>>(short& __n);
      |       ^~~~~~~~
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/istream:172:7: 附注:  conversion of argument 1 would be ill-formed:
/tmp/compiler_gvh2qurl/src:11:12: 错误:invalid conversion from ‘double (*)(double) noexcept’ {aka ‘double (*)(double)’} to ‘short int’ [-fpermissive]
   11 |   cin>>x1>>y1>>x2>>y2;
      |            ^~
      |            |
      |            double (*)(double) noexcept {aka double (*)(double)}
/tmp/compiler_gvh2qurl/src:11:12: 错误:无法将右值‘(short int)y1’绑定到‘short int&’
In file included from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/sstream:38,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/complex:45,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/ccomplex:39,
                 from /nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h:54,
                 from /tmp/compiler_gvh2qurl/src:1:
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/istream:175:7: 附注:candidate: ‘std::basic_istream<_CharT, _Traits>::__istream_type& std::basic_istream<_CharT, _Traits>::operator>>(short unsigned int&) [with _CharT = char; _Traits = std::char_traits; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream]’ 
  175 |       operator>>(unsigned short& __n)
      |       ^~~~~~~~
/nix/store/496xlqhx1mk41sdmrl58xi5y4pa0alys-luogu-gcc-9.3.0/include/c++/9.3.0/istream:175:7: 附注:  conversion of argument 1 would be ill-fo
...

主要错误

‘int y1’ redeclared as different kind of entity

译文

变量(int y1)被重新声明为不同类型的符号。

原因

变量重复定义

1. 变量名重复声明

2. 变量名与头文件包含的函数名重复

本例错误原因

在math.h头文件里 有y1,y0函数

【 c++ 】y1,y0变量名冲突问题_第1张图片

 解决方法

1. 将全局变量放进main函数里,变成局部变量

2. 如果题目中不用math.h,不万能头文件,改写成其他相应的头文件

3. 修改 y1,y0等冲突的变量名

 综上,如用万能头文件或者用math.h,应在声明全局变量时,避免用以上函数名命名全局变量。

你可能感兴趣的:(c++,算法)