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

源代码

#include<bits/stdc++.h>
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<<sum<<endl;
	return 0;
}

编译信息

/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<char>::__istream_type’ {aka ‘std::basic_istream<char>’} and ‘double(double) noexcept’ {aka ‘double(double)’})
   11 |   cin>>x1>>y1>>x2>>y2;
      |   ~~~~~~~^~~~
      |      |     |
      |      |     double(double) noexcept {aka double(double)}
      |      std::basic_istream<char>::__istream_type {aka std::basic_istream<char>}
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<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ <near match>
  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<char>::__istream_type& (*)(std::basic_istream<char>::__istream_type&)’ {aka ‘std::basic_istream<char>& (*)(std::basic_istream<char>&)’} [-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<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>; std::basic_istream<_CharT, _Traits>::__ios_type = std::basic_ios<char>]’ <near match>
  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<char>::__ios_type& (*)(std::basic_istream<char>::__ios_type&)’ {aka ‘std::basic_ios<char>& (*)(std::basic_ios<char>&)’} [-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<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ <near match>
  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<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ <near match>
  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<char>]’ <near match>
  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<char>; std::basic_istream<_CharT, _Traits>::__istream_type = std::basic_istream<char>]’ <near match>
  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函数

82505d4d12b14926982b236fbe912326.png

 解决方法

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

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

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

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

 

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

返返返

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值