Results 1 to 14 of 14

Thread: Initialize a non-static array of pointers in constructor

  1. #1
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Smile Initialize a non-static array of pointers in constructor

    Hello guys

    .h
    #ifndef A_H
    #define A_H

    #include <QObject>
    #include <QList>

    class A : public QObject
    {
    Q_OBJECT
    private:

    private:

    void first();
    void second();
    void third ();
    // and so on

    void(A::*handlers[4])(void);

    public:

    A();
    };
    #endif // A_H
    .cpp
    #include "a.h"

    void A::first()
    {

    }

    void A::second()
    {

    }

    void A::third()
    {

    }

    A::A() : handlers {&A::first, &A::second, &A::third, 0}
    {
    //this is ugly
    handlers[0] = &A::first;
    handlers[1] = &A::second;
    handlers[2] = &A::third;

    //this would be nice
    //handlers[4] = {&A::first,&A::second,&A::third,0};//in static this would work, because it would be like redeclaration, with the type speficier behind
    }
    You see, this code gives me an error.
    I found out that this error does not happen when compiling the code using g++ with -std=c++11 in Cygwin.
    Also, by searching on internet, I found out that this should work.

    I want to initialize that array of pointers in the constructor, but it gives me this error:
    Syntax error - Missing ; before }

    Already placed parentheses, to look like this:
    A::A() : handlers ({&A::first, &A::second, &A::third, 0})
    But it gives me a warning and an error:
    Syntax error - missing ) before {
    Warning - The elements of the array "A :: Handlers" are by default "initialized.

    Is this a Qt problem?
    Thanks

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Initialize a non-static array of pointers in constructor

    Is this a Qt problem?
    No, probably not.

    Your code, as posted without the parentheses, compiles fine here (Linux, gcc 4.8.4) if:
    Qt Code:
    1. CONFIG += c++11
    To copy to clipboard, switch view to plain text mode 
    is in your Qt5 pro file.

    I want to initialize that array of pointers in the constructor, but it gives me this error:
    Syntax error - Missing ; before }
    What is "it"? I assume Windows, but a Microsoft compiler, MingW, ...?

  3. #3
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Initialize a non-static array of pointers in constructor

    Sorry, it's MSVC2010.
    And, it doesn't work. Even with that flag.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Initialize a non-static array of pointers in constructor

    Support For C++11/14/17 Features (Modern C++)
    https://msdn.microsoft.com/en-us/lib...elanguagetable
    Seems your compiler lacks support.

  5. The following user says thank you to ChrisW67 for this useful post:

    Wer_Bn (18th August 2015)

  6. #5
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Initialize a non-static array of pointers in constructor

    Ok
    That list states that 2013 already has initializer lists.
    Can I use Qt with MSVC2013?

  7. #6
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Initialize a non-static array of pointers in constructor

    Hey!
    Finally was able to set Qt with MSVC2013.
    But now the error is:
    cannot specify explicit initializer for arrays

    Even with parantheses, this happens

    I guess it's not possible

  8. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Initialize a non-static array of pointers in constructor

    Can you:
    Qt Code:
    1. handlers = {&A::first, &A::second, &A::third, 0};
    To copy to clipboard, switch view to plain text mode 
    In the body of the constructor and leave it out of the initializer list.

  9. The following user says thank you to ChrisW67 for this useful post:

    Wer_Bn (19th August 2015)

  10. #8
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Initialize a non-static array of pointers in constructor

    "an initializer-list cannot be used as the right operand of this assignment operator"


  11. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Initialize a non-static array of pointers in constructor

    Last thought,
    Qt Code:
    1. void(A::*handlers[4])(void) {&A::first, &A::second, &A::third, 0};
    To copy to clipboard, switch view to plain text mode 
    This is "Non-static data member initializers" but I have not tested the syntax.

  12. The following user says thank you to ChrisW67 for this useful post:

    Wer_Bn (20th August 2015)

  13. #10
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Initialize a non-static array of pointers in constructor

    Ok
    I guess you forgot about the '='

    It's a really complicated syntax, it looks like you are delaring it again, just like static...
    Anyway, I placed that in the constructor. But now there's a new warning:
    'handlers' : local variable is initialized but not referenced

    I'm actually using the function pointer in another method of the class!

  14. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Initialize a non-static array of pointers in constructor

    Quote Originally Posted by Wer_Bn View Post
    Anyway, I placed that in the constructor.
    No, in the header.

    Quote Originally Posted by Wer_Bn View Post
    But now there's a new warning:
    'handlers' : local variable is initialized but not referenced

    I'm actually using the function pointer in another method of the class!
    You cannot use a variable that is local to the constructor's scope in another method. It doesn't exist in that other scope.

    Cheers,
    _

  15. #12
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Initialize a non-static array of pointers in constructor

    Ok
    I know that. The thing is that "handlers" is a private variable of the class.
    I know what's happening in this particular case: I'm declaring a local variable name "handlers", not initializing the private variable "handlers".

    I cannot use that syntax.
    Last edited by Wer_Bn; 24th August 2015 at 10:44.

  16. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Initialize a non-static array of pointers in constructor

    Quote Originally Posted by Wer_Bn View Post
    I cannot use that syntax.
    So it doesn't work when you apply it in the header?

    Cheers,
    _

  17. #14
    Join Date
    Apr 2015
    Posts
    27
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Initialize a non-static array of pointers in constructor

    I have the header file just like posted (1st post).
    The variable "handlers" is indeed in the header, as a private member of class "A".

    But when I use that syntax in the constructor, it declared a local function pointer array named "handlers". Thus, the warning.

    EDIT: I missed your warning: "No, in the header"

    So, I placed the assignment outside of the class scope, in the header, and I get:
    'A::first' : cannot access private member declared in class 'A'
    'A::second' : cannot access private member declared in class 'A'
    'A::third' : cannot access private member declared in class 'A'

    If I do that in the class scope:
    error: C2276: '&' : illegal operation on bound member function expression
    Last edited by Wer_Bn; 25th August 2015 at 09:23.

Similar Threads

  1. Replies: 1
    Last Post: 30th December 2013, 14:53
  2. Replies: 3
    Last Post: 3rd July 2013, 15:07
  3. array of pointers to QtcpSocket
    By gQt in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 16th June 2008, 09:15
  4. Pointer to a 2D array of pointers ??
    By aamer4yu in forum General Programming
    Replies: 2
    Last Post: 1st February 2007, 11:16
  5. array of pointers to functions
    By moowy in forum General Programming
    Replies: 3
    Last Post: 19th October 2006, 10:48

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.