syntax for a tricky class template static member definition

This is a discussion on syntax for a tricky class template static member definition within the C++ Programming forums, part of the General Programming Boards category; I have a class template with an iterator as a static member. Below is a simplified version of the code. ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    835

    syntax for a tricky class template static member definition

    I have a class template with an iterator as a static member. Below is a simplified version of the code. I'm having trouble with the proper syntax for the definition (line 9). Can anyone help?
    Code:
    #include <vector>
    
    template<typename T>
    class A {
      static typename std::vector<typename std::vector<A*>::iterator>::iterator p;
    };
    
    template<typename T>
    typename A<T>::std::vector<std::vector<A<T>*>::iterator>::iterator p;
    
    int main() {}
    Compiling with g++ gives

    foo.cpp:9: error: non-template ‘vector’ used as template
    foo.cpp:9: note: use ‘typename A<T>::std::template vector’ to indicate that it is a template
    foo.cpp:9: error: expected unqualified-id before ‘;’ token

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    it should be:

    Code:
    template<typename T>
    typename std::vector<typename std::vector<A<T>*>::iterator>::iterator A<T>::p;
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    #include <vector>
    
    template<typename T>
    class A {
      static typename std::vector<typename std::vector<A<T>*>::iterator>::iterator p;
    };
    
    template<typename T>
    typename std::vector<typename std::vector<A<T>*>::iterator>::iterator A<T>::p;
    
    int main()
    {
       return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    cpjust: Yes, that works! Thanks. Sebastiani: Yes, I should have seen that, though that change by itself didn't fix it. However, also changing A* to A<T>* in the class declaration as in cpjust's code solved it.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    your right. time to turn up the warnings, I suppose.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,616
    I encountered that problem recently...
    You can't pass a non-specialized class as a template type. Since class A is a template class, just typing A is illegal, instead you to give it a type, so A<T> would fix that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  2. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 06:33 AM
  4. tricky: static int on template class;
    By FillYourBrain in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2002, 03:03 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21