2014-11-23 85 views
0

我试图搜索这个特定的问题,但没有找到任何具体的问题。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 
+1

从错误消息中可以看到已经有一个名为'index'的符号:'char * index(const char *,int)'。所以确实已经宣布。 – MondKin 2014-11-23 01:39:35

回答

1

编译器对这种情况非常详细。索引是具有签名功能的地址的东西

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

请参阅man index(3)。相应的头文件在链中的某处<iostream>

相关问题