簡體   English   中英

使用 function 返回指向函數的指針時出現未聲明的標識符錯誤(Visual Studio 上的 C++)

[英]Undeclared Identifier Error when using function returning pointers to functions (C++ on Visual Studio)

我寫了以下function:

typedef float (*flIntPtr)(char);
flIntPtr c(int i) {
    flIntPtr pt = &b;
    return pt;
}

float b(char c) {
....//do something
}

然后visual studio報告b是一個未聲明的標識符。 我有 go 通過下面的 C2065 的可能原因示例:未聲明的標識符: 編譯器錯誤 C2065

為避免拼寫錯誤或類似問題,我使用單個字母作為 function 名稱,依此類推。 我也經歷了stackoverflow提供的類似問題,在那里我注意到了一個類似的問題,因此我認為這可能是由於錯誤編寫了 function 指針或類型不匹配的表達式,因為我不認為我的 typedef 部分是錯誤的,我試圖改變flIntPtr pt = &b; flIntPtr pt = b ; flIntPtr pt = *b; 但錯誤仍然存在。 因此,我再次請求您的技術支持。~

你的編譯器試圖通過從上到下消化你的代碼來理解它。 這意味着您需要有一個倒樹狀結構(意味着您需要在其頂部的某個位置擁有依賴項的所有依賴項)。 當你沒有它時,編譯器會標記一個錯誤。

int main()
{
    foo();    // <-- Here the compiler doesn't know that a function called foo is there somewhere so it'll flag an error.
    return 0;
}

void foo() {}

為此,您需要的是Forward Declarations 這些基本上告訴編譯器“嘿,伙計,在某處有一個名為 'foo' 的 function。不要標記錯誤,做一個徹頭徹尾的白痴,讓 linker 處理它。”。 它看起來像這樣,

void foo();    // <-- This is the forward declaration.
int main()
{
    foo();    // <-- Now the compiler will leave it alone for the linker to resolve this.
    return 0;
}

void foo() {}

這不僅適用於函數,也適用於類和結構。 當涉及到循環依賴時,這些特別有用。

暫無
暫無

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

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