簡體   English   中英

使用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 ;刪除using namespace std ; 從您的代碼中,並在任何標准函數或對象前加上std::

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM