I wrote the following function:
typedef float (*flIntPtr)(char);
flIntPtr c(int i) {
flIntPtr pt = &b;
return pt;
}
float b(char c) {
....//do something
}
Then visual studio reported that b is an undeclared identifier. I have go through the examples of possible causes to C2065:undeclared identifier below: Compiler Error C2065
To avoid typo or similar problem, I used single letters as function names, and so on. I also went through similiar questions provided by stackoverflow, there I noticed a similar problem hence I thought it might be caused by mistakenly written expression of the function pointer or type mismatch, since I didn't think my typedef part is wrong, I tried to change flIntPtr pt = &b;
to flIntPtr pt = b
; and flIntPtr pt = *b;
but the error remained. Thus, I am once again asking for your technical support.~
Your compiler tries to understand your code by digesting it from the top to bottom. This means that you need to have a inverted tree like structure (meaning that you need to have all the dependencies of a dependent somewhere on the top of it). When you don't have it, the compiler will flag an error.
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() {}
For this, what you need is Forward Declarations . These basically tell the compiler "Hey man, there's a function named 'foo' somewhere. Don't flag an error and be a complete moron let the linker handle it.". It'll look something like this,
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() {}
This is not only for functions, its the same for classes and structs too. These are specially useful when it comes to circular dependencies .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.