473,396 Members | 1,886 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,396 software developers and data experts.

initialization of static data member in header file

Hi there

I am trying to provide a lookup from two 'int's into a char array,
something like this:

template <int g, int estruct Lookup;
template <struct Lookup<0,0{
// some typedef + enums definitions
static const char A;
};
const char Lookup<0,0>::A = "BLA";
// continue with Lookup<0,1; Lookup<0,2...

Since the initialization is done in the .h file, it gets included
multiple times, and at link time I am getting duplicate symbols.

Is there another way to initialize static data members ? Or could
someone let me know of any work around ?

Thanks for suggestion,
-Mathieu
Ps: Full source code is here:
http://gdcm.svn.sourceforge.net/view....h?view=markup
Feb 11 '08 #1
6 5144
mathieu wrote:
I am trying to provide a lookup from two 'int's into a char array,
something like this:

template <int g, int estruct Lookup;
template <struct Lookup<0,0{
// some typedef + enums definitions
static const char A;
};
const char Lookup<0,0>::A = "BLA";
You're trying to initialise an object of type 'char' with a character
array. That's impossible. A char array is not convertible to 'char'.
// continue with Lookup<0,1; Lookup<0,2...

Since the initialization is done in the .h file, it gets included
multiple times, and at link time I am getting duplicate symbols.

Is there another way to initialize static data members ?
Another? Not sure what you mean. Whatever you posted is _not_
a way to initialise it.
Or could
someone let me know of any work around ?
Define and initialise static data in a separate translation unit.
It's not a work-around. It's the only way to do it.
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 11 '08 #2
On Feb 11, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
I am trying to provide a lookup from two 'int's into a char array,
something like this:
template <int g, int estruct Lookup;
template <struct Lookup<0,0{
// some typedef + enums definitions
static const char A;
};
const char Lookup<0,0>::A = "BLA";

You're trying to initialise an object of type 'char' with a character
array. That's impossible. A char array is not convertible to 'char'.
Sorry I meant:

....
static const char A[];
....
const char Lookup<0,0>::A[] = "BLA";

Define and initialise static data in a separate translation unit.
It's not a work-around. It's the only way to do it.
Ok. This might be naive, but I was able to do it with typdef and enum,
I tought I could do the same with a char array...
After all I might have to require user of my lib to link to it,
instead of just including it.

-Mathieu
Feb 11 '08 #3
mathieu wrote:
On Feb 11, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>mathieu wrote:
>> I am trying to provide a lookup from two 'int's into a char array,
something like this:
>>template <int g, int estruct Lookup;
template <struct Lookup<0,0{
// some typedef + enums definitions
static const char A;
};
const char Lookup<0,0>::A = "BLA";

You're trying to initialise an object of type 'char' with a character
array. That's impossible. A char array is not convertible to
'char'.

Sorry I meant:

...
static const char A[];
...
const char Lookup<0,0>::A[] = "BLA";

>Define and initialise static data in a separate translation unit.
It's not a work-around. It's the only way to do it.

Ok. This might be naive, but I was able to do it with typdef and enum,
Not sure what you mean here. Any code sample?
I tought I could do the same with a char array...
After all I might have to require user of my lib to link to it,
instead of just including it.
Static data are special beasts. You can definitely forgo linking if
all you have is code and it's all in templates.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Feb 11 '08 #4
On Feb 11, 4:08 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
On Feb 11, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
I am trying to provide a lookup from two 'int's into a char array,
something like this:
>template <int g, int estruct Lookup;
template <struct Lookup<0,0{
// some typedef + enums definitions
static const char A;
};
const char Lookup<0,0>::A = "BLA";
You're trying to initialise an object of type 'char' with a character
array. That's impossible. A char array is not convertible to
'char'.
Sorry I meant:
...
static const char A[];
...
const char Lookup<0,0>::A[] = "BLA";
Define and initialise static data in a separate translation unit.
It's not a work-around. It's the only way to do it.
Ok. This might be naive, but I was able to do it with typdef and enum,

Not sure what you mean here. Any code sample?
Thanks Victor for taking the time to try to help me out :)
The code is exactly:

http://gdcm.svn.sourceforge.net/view....h?view=markup

Again I'll use the notion of lookup. From a key (a pair of two
uint16_t) I need to provide extra information. Think of it as a yellow
page, from an integer I need to provide a char array (the name).

template <int Numberstruct YellowPage;
template <struct YellowPage<1234567890{
enum { Age = 33 };
static const char Name[];
void Print(std::ostream &os) { os << Name << " is " << Age << " old
\n"; }
};
const char YellowPage<1234567890>::Name[] = "John Doe";

int main()
{
YellowPage<1234567890jd;
jd.Print( std::cout );
return 0;
}

Actually I do even need to expose the 'Name', all I need is really the
'Print' function (to an ostream).
I tought I could do the same with a char array...
After all I might have to require user of my lib to link to it,
instead of just including it.

Static data are special beasts. You can definitely forgo linking if
all you have is code and it's all in templates.
From your last post, I understood that there is no way to initialize
data, enum were just a special case. Maybe with some ifdef blockers
magic, I should be able to include only once the initilization part...

Thanks
-Mathieu
Feb 11 '08 #5
On Feb 11, 4:29 pm, mathieu <mathieu.malate...@gmail.comwrote:
On Feb 11, 4:08 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
On Feb 11, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>mathieu wrote:
>> I am trying to provide a lookup from two 'int's into a char array,
>>something like this:
>>template <int g, int estruct Lookup;
>>template <struct Lookup<0,0{
>> // some typedef + enums definitions
>> static const char A;
>>};
>>const char Lookup<0,0>::A = "BLA";
>You're trying to initialise an object of type 'char' with a character
>array. That's impossible. A char array is not convertible to
>'char'.
Sorry I meant:
...
static const char A[];
...
const char Lookup<0,0>::A[] = "BLA";
>Define and initialise static data in a separate translation unit.
>It's not a work-around. It's the only way to do it.
Ok. This might be naive, but I was able to do it with typdef and enum,
Not sure what you mean here. Any code sample?

Thanks Victor for taking the time to try to help me out :)
The code is exactly:

http://gdcm.svn.sourceforge.net/view...e/DataDictiona...

Again I'll use the notion of lookup. From a key (a pair of two
uint16_t) I need to provide extra information. Think of it as a yellow
page, from an integer I need to provide a char array (the name).

template <int Numberstruct YellowPage;
template <struct YellowPage<1234567890{
enum { Age = 33 };
static const char Name[];
void Print(std::ostream &os) { os << Name << " is " << Age << " old
\n"; }};

const char YellowPage<1234567890>::Name[] = "John Doe";

int main()
{
YellowPage<1234567890jd;
jd.Print( std::cout );
return 0;

}
How about simply:

template <int Numberstruct YellowPage;
template <struct YellowPage<1234567890{
enum { Age = 33 };
const char *GetName() const {
static const char Name[] = "John Doe";
return Name;
}
void Print(std::ostream &os) { os << GetName() << " is " << Age << "
years old\n"; }
};

that should do it.

Thanks all, and sorry for the noise
-Mathieu
Feb 11 '08 #6
On Feb 11, 4:51 pm, mathieu <mathieu.malate...@gmail.comwrote:
On Feb 11, 4:29 pm, mathieu <mathieu.malate...@gmail.comwrote:
On Feb 11, 4:08 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
On Feb 11, 3:29 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
mathieu wrote:
> I am trying to provide a lookup from two 'int's into a char array,
>something like this:
>template <int g, int estruct Lookup;
>template <struct Lookup<0,0{
> // some typedef + enums definitions
> static const char A;
>};
>const char Lookup<0,0>::A = "BLA";
You're trying to initialise an object of type 'char' with a character
array. That's impossible. A char array is not convertible to
'char'.
Sorry I meant:
...
static const char A[];
...
const char Lookup<0,0>::A[] = "BLA";
Define and initialise static data in a separate translation unit.
It's not a work-around. It's the only way to do it.
Ok. This might be naive, but I was able to do it with typdef and enum,
Not sure what you mean here. Any code sample?
Thanks Victor for taking the time to try to help me out :)
The code is exactly:
http://gdcm.svn.sourceforge.net/view...e/DataDictiona...
Again I'll use the notion of lookup. From a key (a pair of two
uint16_t) I need to provide extra information. Think of it as a yellow
page, from an integer I need to provide a char array (the name).
template <int Numberstruct YellowPage;
template <struct YellowPage<1234567890{
enum { Age = 33 };
static const char Name[];
void Print(std::ostream &os) { os << Name << " is " << Age << " old
\n"; }};
const char YellowPage<1234567890>::Name[] = "John Doe";
int main()
{
YellowPage<1234567890jd;
jd.Print( std::cout );
return 0;
}

How about simply:

template <int Numberstruct YellowPage;
template <struct YellowPage<1234567890{
enum { Age = 33 };
const char *GetName() const {
static const char Name[] = "John Doe";
return Name;
}
void Print(std::ostream &os) { os << GetName() << " is " << Age << "
years old\n"; }

};

that should do it.

Thanks all, and sorry for the noise
-Mathieu
Afterall I needed this one:

static const char *GetName() { return "John Doe"; }

-M
Feb 11 '08 #7

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

Similar topics

3
by: Mark Turney | last post by:
Problem: I have a vector full of two different derived class objects (class B and class C) that are derived from the same base class A. I want to loop through vector and invoke a member function...
2
by: Scott J. McCaughrin | last post by:
The following short program compiles fine but fails to link (both on Borland C++ and GNU g++). In particular, g++ gives this link error: "undefined reference to `VarArrar::funct'". It seems as...
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:...
2
by: Sam | last post by:
Hi, I need to initialize a static data member by calling a method of another static member. Like: class A { public: static int TYPE; private static Type _s_type;
7
by: ank | last post by:
Hi, I was curious about how to define static data member of template class. Should I put the definition in a separate source file or in the same header file as its template class? And when this...
3
by: Mike - EMAIL IGNORED | last post by:
MyClass { //I have a static member method: void static myMethod(); //and a static data member: static MyType myData; }; //In the .cpp file: void MyClass::myMethod()
1
by: mangalalei | last post by:
A static data member can be of the same class type as that of which it is a member. A nonstatic data member is restricted to being declared as a pointer or a reference to an object of its class. ...
6
by: Hubert Fritz | last post by:
Hello, I fear I want to have something which is not possible in C++. How is it possible to define a base class so that the derived class is forced to contain a static member variable, which...
6
by: TimTrudeau | last post by:
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...
6
by: gs | last post by:
Hi, I want to know that when memory get allocated to static data member of a class. class A { static int i; }
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
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
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.