Static/NONstatic error!!

J

Jinesh

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

Ron Natalie

Jinesh said:
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).
 
E

E. Robert Tisdale

Jinesh said:
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.
 
B

Boris Rybakov

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

Jinesh

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

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
 
J

Jinesh

Ron Natalie said:
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?
 
D

David Fisher

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

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

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
 
H

Heinz Ozwirk

im Newsbeitrag : 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top