problem with statics variables

M

Martin

I have some classes with statics public variables.... the problem is
that the gcc compiles fine the files but in the linking process gcc
says "multiple definitions of MyClass::Variable" or "undefined
definition of MyClass::Variable". But this problem is only with static
variables within a class. I access to variables with the complete
name. I use kdevelop with pwlib based programs

Thanks
 
V

Victor Bazarov

Martin said:
I have some classes with statics public variables.... the problem is
that the gcc compiles fine the files but in the linking process gcc
says "multiple definitions of MyClass::Variable" or "undefined
definition of MyClass::Variable". But this problem is only with static
variables within a class. I access to variables with the complete
name. I use kdevelop with pwlib based programs

Move the definition of those static variables out of the header. Place
them in a single translation unit (you can even create one specifically
for them).

V
 
U

ulrich

I have some classes with statics public variables.... the problem is
that the gcc compiles fine the files but in the linking process gcc
says "multiple definitions of MyClass::Variable" or "undefined
definition of MyClass::Variable". But this problem is only with static
variables within a class. I access to variables with the complete
name. I use kdevelop with pwlib based programs


sample code would be helpful...

however, imho, you define your static variables in the header and do not
have suiteable include guards ("multiple definitions"), or you do not
define them at all ("undefined definition").
 
H

hibiki

ulrich a écrit :
sample code would be helpful...

however, imho, you define your static variables in the header and do
not have suiteable include guards ("multiple definitions"), or you do
not define them at all ("undefined definition").

The answer is something like :

xxx.h
-----

class XY
{
public:
static string static_string;

....


xxx.cpp
-------

string XY::static_string; (eventually you assign a value here)


--
Salutations,

Joachim Naulet

06 14 90 06 21
http://jnaulet.no-ip.com
 
U

ulrich

ulrich a écrit :

The answer is something like :

xxx.h
-----

class XY
{
public:
static string static_string;

...


xxx.cpp

i would write:

XY.h
---------------------------------------
#if !defined __XY_H__
#define __XY_H__

#include <string>

class XY
{
public:
static std::string static_string;
//...
};

#endif
--------------------------------------


XY.cpp
--------------------------------------

#include <string>
#include "XY.h"

std::string XY::static_string std::string;

//further implementation of class XY follows
//...
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top