簡體   English   中英

C ++編譯器未為未聲明的變量引發錯誤

[英]C++ compiler not throwing error for undeclared variable

我試圖搜索此特定問題,但沒有發現任何具體問題。

我在程序中使用了一個未聲明的變量,並且編譯器沒有抱怨,它只是發出了警告,程序運行正常。 我的gcc版本是4.1.2

下面是我編寫的用於重現此內容的示例程序,未聲明變量“ index”,為什么編譯器將“ index”視為函數,以及在哪里找到該函數的定義?

#include <iostream>
using namespace std;

int testfunction()
{
     try {
         cout << "inside testfunction method\n";
         return 2;
     } catch(...) {
         cout << "caught exception" << index << endl;
     }
     return 1;
 }
 int main()
 {
     cout << "Testfunction return value : " << testfunction() << endl;
 }

編譯:

~ g++ throwreturntest.cpp 
throwreturntest.cpp: In function ���int testfunction()���:
throwreturntest.cpp:11: warning: the address of ���char* index(const char*, int)���, will always evaluate as ���true���

正在運行:

~  ./a.out 
inside testfunction method
Testfunction return value : 2

看起來index是GCC內置函數的名稱: http : //gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html

所以它已經被聲明,只是不是您聲明的。

編譯器對此情況非常詳細。 索引是帶有簽名的函數的地址

char *index(const char *s, int c);

參見man index(3) 相應的標頭位於<iostream>鏈中的某個位置

暫無
暫無

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

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