CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2006
    Posts
    94

    Question Problem declaring static template member

    Here is sample code to give you an idea of what my problem is:

    //FileB.hpp
    Code:
    class CStructClass
    {
        public:
         struct MyStruct
        {
             int paramA;
             int paramB;
        };
    };
    //FileA.hpp
    Code:
    #include FileB.hpp
    
    template<int A, int B>
    class CMyClassA
    {   .
         .
         .
         private:
         static const MyStruct MyArray[];
    }
    //FileA.cpp
    Code:
    #include FileA.hpp
    
    //Now try to initialize the array
    template<int A, int B>
    const CMyClassA<A, B>::MyStruct CMyClassA<A, B>::MyArray[] =
    {
         ...
    }
    I get these errors:

    Warning - CMyClassA<A, B>::MyStruct : dependent name is not a type
    Error - CMyClassA<A, B>::MyStruct : missing storage-class or type specifiers
    Last edited by Ciralia; April 9th, 2007 at 09:58 AM.
    "My software doesn't have bugs, it just develops random features."

  2. #2
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: Problem declaring static template member

    Code:
    CMyClassA<A, B>::MyStruct
    From what you posted... MyStruct is not a local type to CMyClassA...
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Problem declaring static template member

    See the sample.
    Attached Files Attached Files
    Best regards,
    Igor

  4. #4
    Join Date
    Mar 2006
    Posts
    94

    Re: Problem declaring static template member

    Well MyStruct is declared in another file, but it is public in the class it is in. I have modified my sample to help signify this.
    "My software doesn't have bugs, it just develops random features."

  5. #5
    Join Date
    Mar 2006
    Posts
    94

    Re: Problem declaring static template member

    Igor is there any way to put the whole initialization in the .cpp file ?
    "My software doesn't have bugs, it just develops random features."

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Problem declaring static template member

    Quote Originally Posted by Ciralia
    Igor is there any way to put the whole initialization in the .cpp file ?
    It may be harmless for only one case - your .cpp is the only unit that uses this class. Otherwize the static member initialization must reside in header file because of its template nature.
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured