initialization of static data member in header file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mathieu

    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:
    Download Grassroots DICOM for free. Cross-platform DICOM implementation. Grassroots DiCoM is a C++ library for DICOM medical files. It is accessible from Python, C#, Java and PHP.

  • Victor Bazarov

    #2
    Re: initialization of static data member in header file

    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


    Comment

    • mathieu

      #3
      Re: initialization of static data member in header file

      On Feb 11, 3:29 pm, "Victor Bazarov" <v.Abaza...@com Acast.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

      Comment

      • Victor Bazarov

        #4
        Re: initialization of static data member in header file

        mathieu wrote:
        On Feb 11, 3:29 pm, "Victor Bazarov" <v.Abaza...@com Acast.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


        Comment

        • mathieu

          #5
          Re: initialization of static data member in header file

          On Feb 11, 4:08 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
          mathieu wrote:
          On Feb 11, 3:29 pm, "Victor Bazarov" <v.Abaza...@com Acast.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:

          Download Grassroots DICOM for free. Cross-platform DICOM implementation. Grassroots DiCoM is a C++ library for DICOM medical files. It is accessible from Python, C#, Java and PHP.


          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<1234 567890{
          enum { Age = 33 };
          static const char Name[];
          void Print(std::ostr eam &os) { os << Name << " is " << Age << " old
          \n"; }
          };
          const char YellowPage<1234 567890>::Name[] = "John Doe";

          int main()
          {
          YellowPage<1234 567890jd;
          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

          Comment

          • mathieu

            #6
            Re: initialization of static data member in header file

            On Feb 11, 4:29 pm, mathieu <mathieu.malate ...@gmail.comwr ote:
            On Feb 11, 4:08 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
            >
            >
            >
            mathieu wrote:
            On Feb 11, 3:29 pm, "Victor Bazarov" <v.Abaza...@com Acast.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<1234 567890{
            enum { Age = 33 };
            static const char Name[];
            void Print(std::ostr eam &os) { os << Name << " is " << Age << " old
            \n"; }};
            >
            const char YellowPage<1234 567890>::Name[] = "John Doe";
            >
            int main()
            {
            YellowPage<1234 567890jd;
            jd.Print( std::cout );
            return 0;
            >
            }
            How about simply:

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

            that should do it.

            Thanks all, and sorry for the noise
            -Mathieu

            Comment

            • mathieu

              #7
              Re: initialization of static data member in header file

              On Feb 11, 4:51 pm, mathieu <mathieu.malate ...@gmail.comwr ote:
              On Feb 11, 4:29 pm, mathieu <mathieu.malate ...@gmail.comwr ote:
              >
              >
              >
              On Feb 11, 4:08 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
              >
              mathieu wrote:
              On Feb 11, 3:29 pm, "Victor Bazarov" <v.Abaza...@com Acast.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:
              >>
              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<1234 567890{
              enum { Age = 33 };
              static const char Name[];
              void Print(std::ostr eam &os) { os << Name << " is " << Age << " old
              \n"; }};
              >
              const char YellowPage<1234 567890>::Name[] = "John Doe";
              >
              int main()
              {
              YellowPage<1234 567890jd;
              jd.Print( std::cout );
              return 0;
              >
              }
              >
              How about simply:
              >
              template <int Numberstruct YellowPage;
              template <struct YellowPage<1234 567890{
              enum { Age = 33 };
              const char *GetName() const {
              static const char Name[] = "John Doe";
              return Name;
              }
              void Print(std::ostr eam &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

              Comment

              Working...