Results 1 to 6 of 6

Thread: declare array in a class

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default declare array in a class

    Hi, I'd like declare float spec[4]; as private member of a class; my problem is that I need do something about this (declare and initialize but inside declare of class seems not possible):
    Qt Code:
    1. float spec[]={.4f,.4f,.4f,1};
    To copy to clipboard, switch view to plain text mode 
    I tried declare
    Qt Code:
    1. class myClass {
    2. float spec[4];
    3. };
    4.  
    5. myClass::myClass() {
    6. spec[]={.4f,.4f,.4f,1};
    7. }
    To copy to clipboard, switch view to plain text mode 
    but this get an error;
    Do I make a for cicle to initialize spec???? Thanks
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: declare array in a class

    What error do you get ?

    If your variable is static then use :
    Qt Code:
    1. float myClass::spec[] ={...}
    To copy to clipboard, switch view to plain text mode 

    otherwise remove the "[]" in the initialisation.
    Current Qt projects : QCodeEdit, RotiDeCode

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: declare array in a class

    my variabile is not static; but I have only instance of myClass in my program...and the only one instace of spec.....
    I know that I can init an array with '{ }' only when I declare it....
    Regards

  4. #4
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: declare array in a class

    If I declare it static is OK; but I'd like to do it not static:
    Quote Originally Posted by fullmetalcoder
    otherwise remove the "[]" in the initialisation.
    if I remove [] from initialization 'spec = {1,2,2,0};' , get an error:
    Qt Code:
    1. error C2059: syntax error : ']'
    2. error C2143: syntax error : missing ';' before '{'
    3. error C2143: syntax error : missing ';' before '}'
    To copy to clipboard, switch view to plain text mode 
    But is it possible to do this? Thanks
    Regards

  5. #5
    Join Date
    Jan 2006
    Posts
    40
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: declare array in a class

    In the constructor you will have to declare each element of you array seperately.

    Qt Code:
    1. spec[0]=.4f;
    2. spec[1]=.4f;
    3. spec[2]=.4f;
    4. spec[3]=1;
    To copy to clipboard, switch view to plain text mode 

    of course you could employ a loop to make it easier.

    You might then want to develop a constructor that will receive the values in as four arguments.

  6. #6
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: declare array in a class

    I hoped there was a way to don't inittialize array element serarately or with for.....
    Thanks
    Regards

Similar Threads

  1. Replies: 3
    Last Post: 27th December 2008, 19:34
  2. class QHBoxLayout
    By csvivek in forum Installation and Deployment
    Replies: 2
    Last Post: 10th April 2008, 07:57
  3. How to use Signal through direct connection
    By santosh.kumar in forum Qt Programming
    Replies: 1
    Last Post: 14th December 2007, 07:07
  4. Replies: 2
    Last Post: 16th March 2007, 09:04
  5. Replies: 2
    Last Post: 4th May 2006, 19:17

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.