static functions

Discussion in 'C++' started by michael.goossens@gmail.com, Feb 4, 2008.

  1. Guest

    Alright I am implementing a static member function for the first time
    and it is not working :(. Are static member functions implemented in
    the header?

    header:

    static float[4][8] GetPivot(float matrix[4][8], int n);

    cpp:

    static float[4][8] Matrix4x4::GetPivot(float matrix[4][8], int n) {
    ...
    }

    and the errors:

    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.h(12) : error C3409: empty attribute block is
    not allowed
    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.h(12) : error C2143: syntax error : missing ']'
    before 'constant'
    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.h(12) : error C2059: syntax error : 'constant'
    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.h(12) : error C2238: unexpected token(s)
    preceding ';'
    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.cpp(61) : warning C4091: 'static ' : ignored on
    left of 'float' when no variable is declared
    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.cpp(61) : error C2143: syntax error : missing
    ';' before '['
    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.cpp(61) : error C3409: empty attribute block is
    not allowed
    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.cpp(61) : error C2143: syntax error : missing
    ']' before 'constant'
    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.cpp(61) : error C2059: syntax error :
    'constant'
    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.cpp(61) : error C2039: 'GetPivot' : is not a
    member of 'Matrix4x4'
    1> c:\users\michaël\documents\visual studio 2005\projects
    \renderwoman\renderwoman\matrix4x4.h(4) : see declaration of
    'Matrix4x4'
    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.cpp(61) : error C2143: syntax error : missing
    ';' before '{'
    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.cpp(61) : error C2447: '{' : missing function
    header (old-style formal list?)
    1>Transform.cpp
    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.h(12) : error C3409: empty attribute block is
    not allowed
    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.h(12) : error C2143: syntax error : missing ']'
    before 'constant'
    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.h(12) : error C2059: syntax error : 'constant'
    1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    \renderwoman\matrix4x4.h(12) : error C2238: unexpected token(s)
    preceding ';'
     
    , Feb 4, 2008
    #1
    1. Advertising

  2. wrote:
    > Alright I am implementing a static member function for the first time
    > and it is not working :(. Are static member functions implemented in
    > the header?


    Not sure what you mean by "are". Mine aren't _usually_. If they are,
    they are declared 'inline' (or implicitly inline if defined inside the
    class definition).

    >
    > header:
    >
    > static float[4][8] GetPivot(float matrix[4][8], int n);


    You can't return an array.

    >
    > cpp:
    >
    > static float[4][8] Matrix4x4::GetPivot(float matrix[4][8], int n) {


    Drop 'static' from here. And fix the return value type.

    > ...
    > }
    >
    > and the errors:
    >
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.h(12) : error C3409: empty attribute block is
    > not allowed
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.h(12) : error C2143: syntax error : missing ']'
    > before 'constant'
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.h(12) : error C2059: syntax error : 'constant'
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.h(12) : error C2238: unexpected token(s)
    > preceding ';'
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.cpp(61) : warning C4091: 'static ' : ignored on
    > left of 'float' when no variable is declared
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.cpp(61) : error C2143: syntax error : missing
    > ';' before '['
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.cpp(61) : error C3409: empty attribute block is
    > not allowed
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.cpp(61) : error C2143: syntax error : missing
    > ']' before 'constant'
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.cpp(61) : error C2059: syntax error :
    > 'constant'
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.cpp(61) : error C2039: 'GetPivot' : is not a
    > member of 'Matrix4x4'
    > 1> c:\users\michaël\documents\visual studio 2005\projects
    > \renderwoman\renderwoman\matrix4x4.h(4) : see declaration of
    > 'Matrix4x4'
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.cpp(61) : error C2143: syntax error : missing
    > ';' before '{'
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.cpp(61) : error C2447: '{' : missing function
    > header (old-style formal list?)
    > 1>Transform.cpp
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.h(12) : error C3409: empty attribute block is
    > not allowed
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.h(12) : error C2143: syntax error : missing ']'
    > before 'constant'
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.h(12) : error C2059: syntax error : 'constant'
    > 1>c:\users\michaël\documents\visual studio 2005\projects\renderwoman
    > \renderwoman\matrix4x4.h(12) : error C2238: unexpected token(s)
    > preceding ';'


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 4, 2008
    #2
    1. Advertising

  3. Guest

    How do I fix that? Tried to return &matrix instead and catch that as a
    float *, but that didn't work :(.
     
    , Feb 4, 2008
    #3
  4. wrote:
    > How do I fix that? Tried to return &matrix instead and catch that as a
    > float *, but that didn't work :(.


    You can wrap your array in a struct and return that struct.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Feb 4, 2008
    #4
  5. On Feb 4, 8:26 am, "Victor Bazarov" <> wrote:
    > wrote:
    > > How do I fix that? Tried to return &matrix instead and catch that as a
    > > float *, but that didn't work :(.

    >
    > You can wrap your array in a struct and return that struct.
    >
    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask


    Return matrix (not &matrix) and assign that to float*. matrix itself
    is the address of the array. Hope that helps.
     
    C++ Enthusiast, Feb 4, 2008
    #5
  6. James Kanze Guest

    On Feb 4, 5:13 pm, "Victor Bazarov" <> wrote:
    > wrote:


    > > header:


    > > static float[4][8] GetPivot(float matrix[4][8], int n);


    > You can't return an array.


    But you can return a pointer to an array:

    static float (*getPivot( float m[4][8], int n ))[8] ;

    Note that the reinterpretation of array type as pointer type
    doesn't apply to return values. In the above declaration, the
    type of parameter m and the return type are identical.

    Note to that if you write something like:

    float (*getPivot( float m[4][8], int n))[8]
    {
    float result[4][8] ;
    // ...
    return result ;
    }

    you will end up returning a pointer to a local variable. Not a
    good idea either.

    Given this, and the awkward syntax required in the declarations,
    I'd strongly suggest never using C style arrays in this context.
    boost::array would do the trick here (and will be part of the
    next standard); otherwise, at the very least, wrap the array in
    a struct (but I suspect that in the actual application, a full
    class might be even more appropriate).

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Feb 5, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Victor Bazarov

    static and non-static functions

    Victor Bazarov, Apr 2, 2005, in forum: C++
    Replies:
    4
    Views:
    7,257
    Stephen Howe
    Apr 2, 2005
  2. Replies:
    9
    Views:
    982
    Ben Bacarisse
    Feb 6, 2006
  3. Martin Wells

    Static inline functions with static local variables

    Martin Wells, Oct 6, 2007, in forum: C Programming
    Replies:
    10
    Views:
    726
    Army1987
    Oct 8, 2007
  4. mafiQ
    Replies:
    0
    Views:
    484
    mafiQ
    Apr 22, 2009
  5. ssb
    Replies:
    58
    Views:
    1,708
    Chris M. Thomasson
    Sep 14, 2009
Loading...

Share This Page