473,387 Members | 1,579 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Static/NONstatic error!!

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.
Jul 22 '05 #1
8 2941

"Jinesh" <jj******@hotmail.com> wrote in message news:e9**************************@posting.google.c om...
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).

Jul 22 '05 #2
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.

Jul 22 '05 #3
I am not shure, however should default initialization for parameters be in
declaration area ?

--
Best Regards.
Boris Rybakov.
"Jinesh" <jj******@hotmail.com> wrote in message
news:e9**************************@posting.google.c om...
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.

Jul 22 '05 #4
Boris Rybakov wrote:
Should default initialization for parameters be in declaration area?


Yes.

Please don't "top-post".

Jul 22 '05 #5
"Boris Rybakov" <bo******@bezeqint.net> wrote in message news:<3f********@news.bezeqint.net>...
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
Jul 22 '05 #6
"Ron Natalie" <ro*@sensor.com> wrote in message news:<3f***********************@news.newshosting.c om>...
"Jinesh" <jj******@hotmail.com> wrote in message news:e9**************************@posting.google.c om...
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?
Jul 22 '05 #7
"Jinesh" <jj******@hotmail.com> 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
Jul 22 '05 #8
"Jinesh" schrieb im Newsbeitrag news:e9**************************@posting.google.c om...
: 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
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John Bowling | last post by:
I'm trying to get the day of month with: int dayofmonth; dayofmonth = Calendar.get(Calendar.DAY_OF_MONTH); and I get the compile time error of saying get(int) is a not-static method. Is...
4
by: roger | last post by:
Here's a weird one... The code below works just fine when I build in DEBUG mode. Today, I tried to build my solution in RELEASE mode, and immediately fell over this problem - apparently...
10
by: Muffin | last post by:
I am a little new to C# and an have a hard time understanding why I get a nonstatic error. I create an object in my main form that has member properties by using a control. From another form/dialog...
9
Dököll
by: Dököll | last post by:
Does public static main encapsulate a class? Meaning: public static void main(String args) { // public class Myclass // code here to do other stuff
10
by: Jeffrey | last post by:
My understanding is that if you write class X { int y; static int z; }; then you've defined (and declared) X and y, but you have only declared (and not defined) z. If you'd like to...
1
by: JavaJon | last post by:
The problem in the follow code occurs when trying to instantiate an array object. public class Main { /** * @param args the command line arguments */ public static void...
3
by: SLDenman | last post by:
I am getting the "Cannot make a static reference to the non-static method add(Component) from the type Container" error on the last line where I am adding the Welcome label. Why and how do I fix it?...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.