使用memset函数时,对数组的引用是不明确的错误
reference to array is ambiguous error when using memset function
我不明白为什么会出现这个"奇怪"的错误。我读过类似的问题,但它没有回答我的问题。如果我在主函数而不是全局范围内定义数组,就不会有错误。但是假设我必须在全局范围内定义这个数组。为什么我会犯这个错误?这是代码:
#include <iostream>
#include <cstring>
using namespace std;
int right[1005];
int main()
{
memset(right,0,sizeof(right));
return 0;
}
错误如下:
memset2.cpp: In function ‘int main()’:
memset2.cpp:9:9: error: reference to ‘right’ is ambiguous
memset(right,0,sizeof(right));
^
memset2.cpp:6:5: note: candidates are: int right [1005]
int right[1005];
^
In file included from /usr/include/c++/4.8/ios:42:0,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from memset2.cpp:1:
/usr/include/c++/4.8/bits/ios_base.h:924:3: note: std::ios_base& std::right(std::ios_base&)
right(ios_base& __base)
^
memset2.cpp:9:24: error: reference to ‘right’ is ambiguous
memset(right,0,sizeof(right));
^
memset2.cpp:6:5: note: candidates are: int right [1005]
int right[1005];
^
In file included from /usr/include/c++/4.8/ios:42:0,
from /usr/include/c++/4.8/ostream:38,
from /usr/include/c++/4.8/iostream:39,
from memset2.cpp:1:
/usr/include/c++/4.8/bits/ios_base.h:924:3: note: std::ios_base& std::right(std::ios_base&)
right(ios_base& __base)
^
命名空间std
已经命名为right
,并且您通过指令将来自std
的名称包含在全局命名空间中
using namespace std;
因此,为了避免歧义,请使用限定名称
memset( ::right, 0, sizeof( ::right ) );
或者删除该指令,在这种情况下,您可以使用非限定名称right
,因为编译器将只在全局命名空间中查找该名称。
从代码中删除using namespace std ;
,并在任何标准函数或对象之前使用std::
如果以下语句有不明确的错误
memset ( (char*)&mybuf, 0, sizeof(mybuf) );
在内存集之前尝试一下
std::memset ( (char*)&mybuf, 0, sizeof(mybuf) );
相关文章:
- Visual C++(VS2017)中用户定义的转换不明确
- 对"列表"的引用不明确,包括头文件
- 为什么调用具有通用或 r 值引用的重载覆盖函数是不明确的?
- 是否有一种方法可以将C 类成员引用到向量而不明确添加它们
- 由于引用,重载的调用是不明确的
- 关于解引用运算符是否在表达式中产生对象对值的定义不明确
- 当使用g++4.5.2进行编译时,对类的引用是不明确的
- 使用memset函数时,对数组的引用是不明确的错误
- 指定不明确的引用
- 构造函数基本(对距离的引用不明确)
- 错误:在armadillo和oracle occi兼容性问题中,对"剑"的引用不明确
- 引用不明确
- Visual C++编译器为不明确的符号提供了错误的行引用
- 带有重载r值引用函数的调用不明确
- 对Cocos2d-x大小类型的引用不明确
- Cocos2dx导入QuartzCore框架时出现(对Point、Rect、Size的引用不明确)错误
- 通过不明确的转换运算符进行引用绑定
- 到引用的转换不明确
- 升压误差:对'detail'的引用不明确
- 不明确的引用和命名空间(来自两个外部库的定义冲突)