Skip to contentSkip to conversation

"static const" attribute in C++ (How to ?)

DISCUSSION Luciano Moreira - igLnx 3 participants 3 messages Jun 28, 2004 – Jun 29, 2004

lore
Share & tools
lore
#0 lore raw

Sorry for this rookie question, but I'm not a experienced C++ programmer (I m a old C-ANSI programmer, not in C++)

How can us create a kind of "static const" attribute into a class ? I need to use it like as:
char myArray[MYCLASS::DEFSIZE];
Where "DEFSIZE" whould be a "static const" member (or another type) of MYCLASS.

What do I already tried ? Follow:
class MYCLASS {
public:

      static const int DEFSIZE;
};

const int DEFSIZE = 100;

But, when the file "MYCLASS.h" is included more than one file ".cpp", the linker says that the member "MYCLASS::DEFZISE" "was already defined in 'filename'". And don't have occurrences of compiler errors, only on linking.

Thanks,

Luciano

-------------------------------------------
FOLLOW SOME COMPILER FULL ERROR MESSAGE (MSVC++ 6.0):
HMMessageProcessor_Canal.obj : error LNK2005: "public: static unsigned int const MMDadosCarteira::_OFFSET_CARTEIRA2B" (?_OFFSET_CARTEIRA2B@MMDadosCarteira@@2IB) already defined in HMApp.obj

HMMessageProcessor_Canal.obj : error LNK2005: "public: static unsigned int const MUDadosRecarga::_OFFSET_CARTEIRA2B" (?_OFFSET_CARTEIRA2B@MUDadosRecarga@@2IB) already defined in HMApp.obj

PS: I didn't code "static unsigned int const". In my source code I coded "static const unsigned" only. I don't know why MSVC "translated" my code.

#1 lore raw
Quoted 6 lines · Luciano Moreira - igLnx

Sorry for this rookie question, but I'm not a experienced C++ programmer (I m a old C-ANSI programmer, not in C++)

How can us create a kind of "static const" attribute into a class ? I need to use it like as:
char myArray[MYCLASS::DEFSIZE];

That won't work. Array sizes have to be constant expressions (expressions which contain only literal values).

Quoted 8 lines

Where "DEFSIZE" whould be a "static const" member (or another type) of MYCLASS.

What do I already tried ? Follow:

class MYCLASS {
    public:
       static const int DEFSIZE;
};
Quoted 6 lines

const int DEFSIZE = 100;

But, when the file "MYCLASS.h" is included more than one file ".cpp", the linker says that the member "MYCLASS::DEFZISE" "was already defined in 'filename'". And don't have occurrences of compiler errors, only on linking.

Definitions belong in source (.cpp) files, not header files.

Signature · 1 line
Glynn Clements <[email protected]>
#2 lore raw
In reply to Luciano Moreira - igLnx
Quoted 9 lines · Luciano Moreira - igLnx

Sorry for this rookie question, but I'm not a experienced C++ programmer (I m a old C-ANSI programmer, not in C++)

How can us create a kind of "static const" attribute into a class ? I need to use it like as:
char myArray[MYCLASS::DEFSIZE];
Where "DEFSIZE" whould be a "static const" member (or another type) of MYCLASS.

Quoted 7 lines

What do I already tried ? Follow:

class MYCLASS {
   public:
      static const int DEFSIZE;
};

const int DEFSIZE = 100;

The line setting the value should in a .cpp file not a header. You may only set the value once for a variable during compilation (the value is put in the actual file, not generated on startup - the data section)

I am not sure if a static const may be used for array sizes? IIRC it is possible under c++ and not on c but far away from sure on that.

Another option is to define the value as an enum and then you still get type checking and you can encapsulate it in a class unlike a define, but it acts like a define for all other purposes (you can use it as a test option for a case statement).

Quoted 32 lines

But, when the file "MYCLASS.h" is included more than one file ".cpp", the linker says that the member "MYCLASS::DEFZISE" "was already defined in 'filename'". And don't have occurrences of compiler errors, only on linking.

Thanks,

Luciano

-------------------------------------------
FOLLOW SOME COMPILER FULL ERROR MESSAGE (MSVC++ 6.0):
HMMessageProcessor_Canal.obj : error LNK2005: "public: static unsigned int const MMDadosCarteira::_OFFSET_CARTEIRA2B" (?_OFFSET_CARTEIRA2B@MMDadosCarteira@@2IB) already defined in HMApp.obj

HMMessageProcessor_Canal.obj : error LNK2005: "public: static unsigned int const MUDadosRecarga::_OFFSET_CARTEIRA2B" (?_OFFSET_CARTEIRA2B@MUDadosRecarga@@2IB) already defined in HMApp.obj

PS: I didn't code "static unsigned int const". In my source code I coded "static const unsigned" only. I don't know why MSVC "translated" my code.

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

+++++++++++++++++++++++++++++++++++++++++++
This Mail Was Scanned By Mail-seCure System
at the Tel-Aviv University CC.