Static/NONstatic error!!

Discussion in 'C++' started by Jinesh, Jan 5, 2004.

  1. Jinesh

    Jinesh Guest

    I illustrate the compiler error I get using the following example.
    ---------------------------------------------------------------
    Class ClassName
    {
    private:
    static const int constVarName = 100;
    void functionName(int parameterName)
    };

    void ClassName::functionName(int parameterName=constVarName)
    {
    some code;
    }
    ---------------------------------------------------------------
    On compiling, I get the error "A nonstatic member reference must be
    relative to a specific object". I have to initialize the variable in
    the header file (and not in the member initialization list of the
    compiler as that causes other errors). I am using the CC compiler on
    IRIX(SGI - Fuel).

    Kindly post your comments. Thanks for your participation.
     
    Jinesh, Jan 5, 2004
    #1
    1. Advertising

  2. Jinesh

    Ron Natalie Guest

    "Jinesh" <> wrote in message news:...
    > I illustrate the compiler error I get using the following example.
    > ---------------------------------------------------------------
    > Class ClassName
    > {
    > private:
    > static const int constVarName = 100;
    > void functionName(int parameterName)
    > };
    >
    > void ClassName::functionName(int parameterName=constVarName)
    > {
    > some code;
    > }


    I can't vouch for the error you got (since you don't provide it full information), however
    I suggest that you do not want to put the default parameter in the implementation of the
    functionName method. The default params MUST be seen in the context that the function
    is CALLED (it is there that the default argument is substituted).
     
    Ron Natalie, Jan 5, 2004
    #2
    1. Advertising

  3. Jinesh wrote:

    > I illustrate the compiler error I get using the following example.
    > ---------------------------------------------------------------
    > class ClassName {
    > private:
    > static const int constVarName = 100;
    > void functionName(int parameterName);
    > };
    >
    > void ClassName::functionName(int parameterName=constVarName) {
    > some code;
    > }
    > ---------------------------------------------------------------
    > On compiling, I get the error "A nonstatic member reference must be
    > relative to a specific object". I have to initialize the variable in
    > the header file (and not in the member initialization list of the
    > compiler as that causes other errors). I am using the CC compiler on
    > IRIX(SGI - Fuel).
    >
    > Kindly post your comments. Thanks for your participation.



    > cat ClassName.cc

    class ClassName {
    private:
    static const
    int constVarName = 100;
    void functionName(int parameterName);
    };

    void ClassName::functionName(int parameterName=constVarName) {
    // some code;
    }

    > g++ -Wall -ansi -pedantic -O2 -c ClassName.cc

    ClassName.cc:1: warning: \
    all member functions in class `ClassName' are private

    Sorry, I don't get any diagnostic of the form mentioned above.
     
    E. Robert Tisdale, Jan 5, 2004
    #3
  4. I am not shure, however should default initialization for parameters be in
    declaration area ?

    --
    Best Regards.
    Boris Rybakov.
    "Jinesh" <> wrote in message
    news:...
    > I illustrate the compiler error I get using the following example.
    > ---------------------------------------------------------------
    > Class ClassName
    > {
    > private:
    > static const int constVarName = 100;
    > void functionName(int parameterName)
    > };
    >
    > void ClassName::functionName(int parameterName=constVarName)
    > {
    > some code;
    > }
    > ---------------------------------------------------------------
    > On compiling, I get the error "A nonstatic member reference must be
    > relative to a specific object". I have to initialize the variable in
    > the header file (and not in the member initialization list of the
    > compiler as that causes other errors). I am using the CC compiler on
    > IRIX(SGI - Fuel).
    >
    > Kindly post your comments. Thanks for your participation.
     
    Boris Rybakov, Jan 5, 2004
    #4
  5. Boris Rybakov wrote:

    > Should default initialization for parameters be in declaration area?


    Yes.

    Please don't "top-post".
     
    E. Robert Tisdale, Jan 5, 2004
    #5
  6. Jinesh

    Jinesh Guest

    "Boris Rybakov" <> wrote in message news:<3ff9c0c0$>...
    > I am not shure, however should default initialization for parameters be in
    > declaration area ?
    >
    > --
    > Best Regards.
    > Boris Rybakov.


    Boris: The default parameter initialization for a function can appear
    either in the header file (prototype declaration) or the code file
    (implementation) but NOT both.

    Everyone else: Thanks. What more information should I provide? I could
    post the actual code but the issue at hand would be lost in the
    intense complicity of the code. Ask me and I shall post the
    information as requested.

    Jinesh
     
    Jinesh, Jan 5, 2004
    #6
  7. Jinesh

    Jinesh Guest

    "Ron Natalie" <> wrote in message news:<3ff9a5ec$0$32333$>...
    > "Jinesh" <> wrote in message news:...
    > > I illustrate the compiler error I get using the following example.
    > > ---------------------------------------------------------------
    > > Class ClassName
    > > {
    > > private:
    > > static const int constVarName = 100;
    > > void functionName(int parameterName)
    > > };
    > >
    > > void ClassName::functionName(int parameterName=constVarName)
    > > {
    > > some code;
    > > }

    >
    > I can't vouch for the error you got (since you don't provide it full information), however
    > I suggest that you do not want to put the default parameter in the implementation of the
    > functionName method. The default params MUST be seen in the context that the function
    > is CALLED (it is there that the default argument is substituted).


    That makes sense. I applied the changes. Same error though? Any more ideas?
     
    Jinesh, Jan 5, 2004
    #7
  8. Jinesh

    David Fisher Guest

    "Jinesh" <> wrote:

    > Class ClassName
    > {
    > private:
    > static const int constVarName = 100;
    > void functionName(int parameterName)
    > };
    >
    > void ClassName::functionName(int parameterName=constVarName)
    > {
    > some code;
    > }
    > ---------------------------------------------------------------
    > On compiling, I get the error "A nonstatic member reference must be
    > relative to a specific object"


    I looked up the error message with google, and found the following example:

    bar.cc(9): error: a nonstatic member reference must be relative to a
    specific object
    list_t l2 = list_t::list (l.begin (), l.end ());

    The problem with this line is that list_t::list is being called like a
    static member function,
    when it is a normal member function (ie. it must be called with an object).

    None of the code you posted seems to do this, but I would guess that the
    problem is
    either calling a member function or using a member variable without an
    object ...
    for example, you might have said:

    ClassName::functionName(val)

    instead of:

    ClassName someObject;
    someObject.functionName(val)

    Or, if you actually wanted to call ClassName::functionName() without an
    object,
    you would need to say "static" in declaration of the function.

    David F
     
    David Fisher, Jan 6, 2004
    #8
  9. Jinesh

    Heinz Ozwirk Guest

    "Jinesh" schrieb im Newsbeitrag news:...
    : I illustrate the compiler error I get using the following example.
    : ---------------------------------------------------------------
    : Class ClassName
    : {
    : private:
    : static const int constVarName = 100;
    : void functionName(int parameterName)
    : };
    :
    : void ClassName::functionName(int parameterName=constVarName)
    : {
    : some code;
    : }
    : ---------------------------------------------------------------
    : On compiling, I get the error "A nonstatic member reference must be
    : relative to a specific object". I have to initialize the variable in
    : the header file (and not in the member initialization list of the
    : compiler as that causes other errors). I am using the CC compiler on
    : IRIX(SGI - Fuel).
    :
    : Kindly post your comments. Thanks for your participation.

    1. Declare constVarName as public.
    2. Use ClassName::constVarName as the default parameter.

    Remember, default parameters must be seen in the context of the caller.

    ClassName obj;
    obj.functionName();

    is basically the same as

    ClassName obj;
    obj.functionName(constVarName);

    HTH
    Heinz
     
    Heinz Ozwirk, Jan 6, 2004
    #9
    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. Pedro López
    Replies:
    0
    Views:
    430
    Pedro López
    Oct 13, 2003
  2. Lucy Ludmiller
    Replies:
    3
    Views:
    674
    Thomas Tutone
    Sep 1, 2006
  3. The|Godfather
    Replies:
    7
    Views:
    3,952
    Miles Bader
    Oct 24, 2006
  4. Jeffrey
    Replies:
    7
    Views:
    415
    James Kanze
    Sep 30, 2008
  5. Computerjunkie
    Replies:
    0
    Views:
    956
    Computerjunkie
    Apr 6, 2012
Loading...

Share This Page