2017-05-12 51 views
-7

下面是我的一些C++代碼。我得到這個錯誤:本地函數定義不合法

C2601: 'main' : local function definitions are illegal

爲什麼我得到這個?

BOOL CBasicApp::InitInstance() 
{ 
    typedef BOOL (__stdcall *pFunc)(); 

    int main(int argc, char* argv[]) 
    { 
     pFunc pf = 0; 

     HMODULE hMod = 0; 

     hMod = LoadLibrary("dbgghelp.dll"); 

     if (!hMod) 
     { 
      printf("File not found: Dbgghelp.DLL\n"); 
      return 0; 

     } 

     pf = GetProcAddress(hMod,"L100A6F95"); 
+0

嘗試移動typedef出方法。將其放入.h文件或導入後。 – Tav

+5

我認爲錯誤信息很清楚。在C++中,你不能在本地定義一個函數(即直接在另一個函數中)。 – cpplearner

回答

0

main是控制檯應用程序的入口點函數。它必須在全球範圍內。無論如何,你不能在其他函數中嵌套函數。

嘗試一些更喜歡這個:

BOOL CBasicApp::InitInstance() 
{ 
    typedef BOOL (__stdcall *pFunc)(); 

    HMODULE hMod = LoadLibrary("dbgghelp.dll"); 
    if (!hMod) 
    { 
     printf("Error loading dbgghelp.DLL\n"); 
     return FALSE; 
    } 

    pFunc pf = GetProcAddress(hMod, "L100A6F95"); 
    if (!pf) 
    { 
     printf("Error finding L100A6F95 function\n"); 
     FreeLibrary(hMod); 
     return FALSE; 
    } 

    if (!pf()) 
    { 
     printf("L100A6F95 function failed\n"); 
     FreeLibrary(hMod); 
     return FALSE; 
    } 

    FreeLibrary(hMod); 
    return TRUE; 
} 

... 

int main(int argc, char* argv[]) 
{ 
    CBasicApp app; 
    if (app.InitInstance()) 
    { 
     ... 
    } 
    else 
    { 
     ... 
    } 
} 
3

你需要移動main功能出InitInstance函數體。

在C++中,不可能在其他函數內部定義嵌套函數(lambda函數除外,但它們是表達式,而不是函數定義)。

0

哥們感謝所有誰發表意見和幫助剛剛加入到我的.h /頭文件,下面的代碼

typedef BOOL (__stdcall *pFunc)(); 
int main(int argc, char* argv[]); 

..fixed我

public: 
virtual BOOL InitInstance();` 

那麼它的成功編譯沒有錯誤......但現在我的問題是爲什麼我的mainapp.exe不是執行時,即時通訊啓動順便啓動btw我的dgghelp.dll是使用ce,宏和其他我只是重命名它的遊戲保護...它有一些我認爲轉儲代碼裏面....