Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sharing Common objects - warning "defined but not used"

I have a number of C++ classes, alot of them (not all) share two "static size variables" e.g.

share.h

/*Other variables in this header used by all classes*/ 

static size width=10;//Used by about 60% 

static size height = 12;//used by about 60% 

So I placed them in a header file along with other objects that all classes share.

when I compile the project I get alot of warnings (from the classes which dont use these), which complain about them being defined and not used. But I need them there!

So I ask, is there a way to define these, so that classes not using these two variables can use this header file without throwing warnings about them not being defined?

Thank you in advance

like image 283
Perl_noob Avatar asked Oct 24 '11 11:10

Perl_noob


1 Answers

Either declare them const, or declare them extern and define them in exactly one source file. A compiler should expect constants to be defined (in header files) but not used, and not give a warning for that.

Defining static variables that you don't use is often a sign of an error, so the warning is useful in that case. (If you actually do want separate, modifiable copies of these variables in multiple translation units, then you should probably rethink your program design).

like image 98
Mike Seymour Avatar answered Oct 09 '22 10:10

Mike Seymour