Static/NONstatic error!!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jinesh

    Static/NONstatic error!!

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

    void ClassName::func tionName(int parameterName=c onstVarName)
    {
    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.
  • Ron Natalie

    #2
    Re: Static/NONstatic error!!


    "Jinesh" <jjjj7777@hotma il.com> wrote in message news:e9120c17.0 401050949.46a2b 5cf@posting.goo gle.com...[color=blue]
    > I illustrate the compiler error I get using the following example.
    > ---------------------------------------------------------------
    > Class ClassName
    > {
    > private:
    > static const int constVarName = 100;
    > void functionName(in t parameterName)
    > };
    >
    > void ClassName::func tionName(int parameterName=c onstVarName)
    > {
    > some code;
    > }[/color]

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

    Comment

    • E. Robert Tisdale

      #3
      Re: Static/NONstatic error!!

      Jinesh wrote:
      [color=blue]
      > I illustrate the compiler error I get using the following example.
      > ---------------------------------------------------------------
      > class ClassName {
      > private:
      > static const int constVarName = 100;
      > void functionName(in t parameterName);
      > };
      >
      > void ClassName::func tionName(int parameterName=c onstVarName) {
      > 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.[/color]

      [color=blue]
      > cat ClassName.cc[/color]
      class ClassName {
      private:
      static const
      int constVarName = 100;
      void functionName(in t parameterName);
      };

      void ClassName::func tionName(int parameterName=c onstVarName) {
      // some code;
      }
      [color=blue]
      > g++ -Wall -ansi -pedantic -O2 -c ClassName.cc[/color]
      ClassName.cc:1: warning: \
      all member functions in class `ClassName' are private

      Sorry, I don't get any diagnostic of the form mentioned above.

      Comment

      • Boris Rybakov

        #4
        Re: Static/NONstatic error!!

        I am not shure, however should default initialization for parameters be in
        declaration area ?

        --
        Best Regards.
        Boris Rybakov.
        "Jinesh" <jjjj7777@hotma il.com> wrote in message
        news:e9120c17.0 401050949.46a2b 5cf@posting.goo gle.com...[color=blue]
        > I illustrate the compiler error I get using the following example.
        > ---------------------------------------------------------------
        > Class ClassName
        > {
        > private:
        > static const int constVarName = 100;
        > void functionName(in t parameterName)
        > };
        >
        > void ClassName::func tionName(int parameterName=c onstVarName)
        > {
        > 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.[/color]


        Comment

        • E. Robert Tisdale

          #5
          Re: Static/NONstatic error!!

          Boris Rybakov wrote:
          [color=blue]
          > Should default initialization for parameters be in declaration area?[/color]

          Yes.

          Please don't "top-post".

          Comment

          • Jinesh

            #6
            Re: Static/NONstatic error!!

            "Boris Rybakov" <boris731@bezeq int.net> wrote in message news:<3ff9c0c0$ 1@news.bezeqint .net>...[color=blue]
            > I am not shure, however should default initialization for parameters be in
            > declaration area ?
            >
            > --
            > Best Regards.
            > Boris Rybakov.[/color]

            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

            Comment

            • Jinesh

              #7
              Re: Static/NONstatic error!!

              "Ron Natalie" <ron@sensor.com > wrote in message news:<3ff9a5ec$ 0$32333$9a6e19e a@news.newshost ing.com>...[color=blue]
              > "Jinesh" <jjjj7777@hotma il.com> wrote in message news:e9120c17.0 401050949.46a2b 5cf@posting.goo gle.com...[color=green]
              > > I illustrate the compiler error I get using the following example.
              > > ---------------------------------------------------------------
              > > Class ClassName
              > > {
              > > private:
              > > static const int constVarName = 100;
              > > void functionName(in t parameterName)
              > > };
              > >
              > > void ClassName::func tionName(int parameterName=c onstVarName)
              > > {
              > > some code;
              > > }[/color]
              >
              > 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).[/color]

              That makes sense. I applied the changes. Same error though? Any more ideas?

              Comment

              • David Fisher

                #8
                Re: Static/NONstatic error!!

                "Jinesh" <jjjj7777@hotma il.com> wrote:
                [color=blue]
                > Class ClassName
                > {
                > private:
                > static const int constVarName = 100;
                > void functionName(in t parameterName)
                > };
                >
                > void ClassName::func tionName(int parameterName=c onstVarName)
                > {
                > some code;
                > }
                > ---------------------------------------------------------------
                > On compiling, I get the error "A nonstatic member reference must be
                > relative to a specific object"[/color]

                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::func tionName(val)

                instead of:

                ClassName someObject;
                someObject.func tionName(val)

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

                David F


                Comment

                • Heinz Ozwirk

                  #9
                  Re: Static/NONstatic error!!

                  "Jinesh" schrieb im Newsbeitrag news:e9120c17.0 401050949.46a2b 5cf@posting.goo gle.com...
                  : I illustrate the compiler error I get using the following example.
                  : ---------------------------------------------------------------
                  : Class ClassName
                  : {
                  : private:
                  : static const int constVarName = 100;
                  : void functionName(in t parameterName)
                  : };
                  :
                  : void ClassName::func tionName(int parameterName=c onstVarName)
                  : {
                  : 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::cons tVarName as the default parameter.

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

                  ClassName obj;
                  obj.functionNam e();

                  is basically the same as

                  ClassName obj;
                  obj.functionNam e(constVarName) ;

                  HTH
                  Heinz

                  Comment

                  Working...