-
February 10th, 2021, 03:11 AM
#1
structure with function pointers
Hi,
compiled the following code with visual studio 2102 and got an error in the red line.
Code:
#include "stdafx.h"
#include <iostream>
struct MyStruct {
int (*foo)();
};
static int func1()
{
printf("I am called\n");
return 0;
}
static struct MyStruct a = {
.foo = func1 //getting error here as shown below
};
int main()
{
a.foo();
return 0;
}
Error 1 error C2143: syntax error : missing '}' before '.'
Error 2 error C2143: syntax error : missing ';' before '.'
Error 3 error C2059: syntax error : '.'
Error 4 error C2143: syntax error : missing ';' before '}'
Error 5 error C2059: syntax error : '}'
6 IntelliSense: expected an expression
-
February 10th, 2021, 04:50 AM
#2
Re: structure with function pointers
Code:
struct MyStruct {
int (*foo)();
};
static int func1()
{
printf("I am called\n");
return 0;
}
static struct MyStruct a = {
.foo = func1
};
int main()
{
a.foo();
return 0;
}
Compiles OK as C++20 with VS2019.
What is VS 2102?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 10th, 2021, 09:37 AM
#3
Re: structure with function pointers
my typo error, meant Visual studio 2012.
I am not why it fails with VS2012?. how to set the VS2012 to C++20?. Project-?Properties?.
-
February 10th, 2021, 09:49 AM
#4
Re: structure with function pointers
Originally Posted by @EE@
how to set the VS2012 to C++20?. Project-?Properties?.
I am not sure it is possible.
Look at the General Properties -> C++ Language Standard
Victor Nijegorodov
-
February 10th, 2021, 12:23 PM
#5
Re: structure with function pointers
Originally Posted by @EE@
my typo error, meant Visual studio 2012.
I am not why it fails with VS2012?. how to set the VS2012 to C++20?. Project-?Properties?.
You can't. C++20 compatibility is only available with VS2019.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|