473,385 Members | 2,274 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,385 software developers and data experts.

Static Data Members

I am getting the following error from g++ ...

./include/WatchTable_class.h:23: error: invalid use of non-static data member `WatchTable::start'

Here is the class header file ...

#ifndef WatchTable_class_h
#define WatchTable_class_h

#include "./watch_class.h"

class WatchTable
{
private:
Watch* start; //<<<<<<<<<<<<<<<<
int tableLength;

public:
WatchTable();
~WatchTable();
...
};

The indicated line is where the problem occurs. I am trying to create a pointer to an object of class Watch named start. I can find nothing on the web about what causes this error, but if I change the line to read...

static Watch* start; //<<<<<<<<<<<<<<<<

the error goes away %-) but then I get a link error...

undefined reference to `WatchTable::start'

I have no idea why the compiler wants start to be a static pointer, or why the linker can not find WatchTable::start after I changed it to static.

Thanks in advance for any help. I have been pouring over my books and the web but I can't get my head around this one.
Apr 24 '07 #1
6 9812
Static Data Members
--------------------------------------------------------------------------------

I am getting the following error from g++ ...

./include/WatchTable_class.h:23: error: invalid use of non-static data member `WatchTable::start'

Here is the class header file ...

Expand|Select|Wrap|Line Numbers
  1. #ifndef WatchTable_class_h
  2. #define WatchTable_class_h
  3.  
  4. #include "./watch_class.h"
  5.  
  6. class WatchTable
  7. {
  8. private:
  9. Watch* start; //<<<<<<<<<<<<<<<<
  10. int tableLength;
  11.  
  12. public:
  13. WatchTable();
  14. ~WatchTable();
  15. ...
  16. };
  17.  
The indicated line is where the problem occurs. I am trying to create a pointer to an object of class Watch named start. I can find nothing on the web about what causes this error, but if I change the line to read...

Expand|Select|Wrap|Line Numbers
  1. static Watch* start; //<<<<<<<<<<<<<<<<
  2.  
the error goes away %-) but then I get a link error...

undefined reference to `WatchTable::start'

I have no idea why the compiler wants start to be a static pointer, or why the linker can not find WatchTable::start after I changed it to static.

Thanks in advance for any help. I have been pouring over my books and the web but I can't get my head around this one.
Apr 24 '07 #2
r035198x
13,262 8TB
Please do not double post.

Threads merged.
Apr 24 '07 #3
gpraghuram
1,275 Expert 1GB
Hi,
If you are using any static variable inside a class then the variable needs to be initialized outside the class, otherwise you will get a linker error.

Init like this
Watch* WatchTable::start=NULL;

will solve the linker error.

If u can post a snippet of the the header file then it will be helpful to find the issue.
Thanks
Raghuram
Apr 24 '07 #4
Banfa
9,065 Expert Mod 8TB
I don't think making start static will solve your problem as you then only get 1 instance of the member for all instances of the class. The compiler does not 'want' start to be static, you have just randomly added the keyword without understanding and fixing the problem.

The line of code you indicate looks good, I assume that Watch is defined in watch_class.h, presumably as a class from the name of the header file. Which suggests the error is else where in your code.

Did you get any other warnings or errors that you have not reported?
Apr 24 '07 #5
Making the Watch member variable static is the not the solution.

Compiler is not getting the definition of Watch which i supposed should be in Watch_Class header file. Can u print the error u get which u get when u make the Watch member variable non-static or print the snippet of header file Watch_Class.
Apr 24 '07 #6
Thanks for the help to all. I found the problem. It is correct that I did not want start to be static. The compiler error message had me confused. The problem was caused because I tried use the class member 'start' as a default parameter value in one of the member functions:
Expand|Select|Wrap|Line Numbers
  1. Watch* find(EUI64& _eiu, Watch* _w=start);
  2.  

I changed the line above to:
Expand|Select|Wrap|Line Numbers
  1. Watch* find(EUI64& _eiu, Watch* _w=NULL);
  2.  

and now all is good!
May 20 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
8
by: Scott J. McCaughrin | last post by:
The following program compiles fine but elicits this message from the linker: "undefined reference to VarArray::funct" and thus fails. It seems to behave as if the static data-member:...
8
by: Srini | last post by:
Hello all, I was just wondering about this. A const member function guarantees constness of the object within the function body. But there's no way for a member function to guarantee the...
6
by: lovecreatesbeauty | last post by:
Hello Experts, Why static data members can be declared as the type of class which it belongs to? Inside a class, non-static data members such as pointers and references can be declared as...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
11
by: dee | last post by:
OleDbCommand class like many .NET classes has the following description in its help file: "Thread Safety Any public static (Shared in Visual Basic) members of this type are safe for...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
7
by: Jon Vaughan | last post by:
I have a piece of code that I want to run on a Pocket Pc, I have written a data class that will store the small amount of data that is required for the program. As this class will be used via a few...
8
by: crjjrc | last post by:
Hi, I've got a base class and some derived classes that look something like this: class Base { public: int getType() { return type; } private: static const int type = 0; };
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.