我試圖搜索這個特定的問題,但沒有找到任何具體的問題。C++編譯器不會爲未聲明的變量拋出錯誤
我在我的程序中使用了一個未聲明的變量,編譯器沒有抱怨,它只是給出了一個警告,程序運行良好。我的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'的符號:'char * index(const char *,int)'。所以確實已經宣佈。 – MondKin 2014-11-23 01:39:35