static member of a templated class weirdness

Discussion in 'C++' started by Jacek Dziedzic, Dec 7, 2005.

  1. Hello!

    Is this program syntactically correct?

    // -------------------------------
    template <typename T>
    class Foo {
    static const int len=5;
    static const char ID[len];
    };

    template<typename T> const char Foo<T>::ID[len]={'1','2','3','4','\0'};

    int main() {
    Foo<double> foo;
    }
    // -------------------------------

    It does compile with Intel's compiler, but g++ 3.3.4 reports

    test3.cpp:8: error: conflicting types for `const char
    Foo<T>::ID[Foo<T>::len]'
    test3.cpp:5: error: previous declaration as `const char
    Foo<T>::ID[Foo<T>::len]

    It looks like the declarations reported are identical,
    so what is it complaining about?

    If the code is wrong, how can I fix it?

    If the code is OK, is there a way to go around this
    (apart from changing to a different compiler)?

    TIA,
    - J.
     
    Jacek Dziedzic, Dec 7, 2005
    #1
    1. Advertisements

  2. Jacek Dziedzic

    mlimber Guest

    Jacek Dziedzic wrote:
    > Hello!
    >
    > Is this program syntactically correct?
    >
    > // -------------------------------
    > template <typename T>
    > class Foo {
    > static const int len=5;
    > static const char ID[len];
    > };
    >
    > template<typename T> const char Foo<T>::ID[len]={'1','2','3','4','\0'};
    >
    > int main() {
    > Foo<double> foo;
    > }
    > // -------------------------------
    >
    > It does compile with Intel's compiler, but g++ 3.3.4 reports
    >
    > test3.cpp:8: error: conflicting types for `const char
    > Foo<T>::ID[Foo<T>::len]'
    > test3.cpp:5: error: previous declaration as `const char
    > Foo<T>::ID[Foo<T>::len]
    >
    > It looks like the declarations reported are identical,
    > so what is it complaining about?
    >
    > If the code is wrong, how can I fix it?
    >
    > If the code is OK, is there a way to go around this
    > (apart from changing to a different compiler)?
    >
    > TIA,
    > - J.


    Looks like a bug in that version of g++. You could always try a
    relatively minor upgrade. I have 3.4.4 and it works fine.

    Cheers! --M
     
    mlimber, Dec 7, 2005
    #2
    1. Advertisements

  3. mlimber wrote:
    >
    > Looks like a bug in that version of g++. You could always try a
    > relatively minor upgrade. I have 3.4.4 and it works fine.


    Yes, it helped. Thanks a lot for a quick solution!

    - J.
     
    Jacek Dziedzic, Dec 7, 2005
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Siemel Naran
    Replies:
    4
    Views:
    920
    Micah Cowan
    Jan 12, 2005
  2. steve
    Replies:
    4
    Views:
    622
    Brian van den Broek
    Mar 13, 2005
  3. Replies:
    1
    Views:
    387
  4. dolphin
    Replies:
    3
    Views:
    1,516
    Pete Becker
    Dec 5, 2007
  5. chhenning
    Replies:
    5
    Views:
    464
    chhenning
    Feb 13, 2008
Loading...

Share This Page