CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2019
    Posts
    72

    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

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  3. #3
    Join Date
    Aug 2019
    Posts
    72

    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?.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: structure with function pointers

    Quote Originally Posted by @EE@ View Post
    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

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: structure with function pointers

    Quote Originally Posted by @EE@ View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured