Strange Templates Error

This is a discussion on Strange Templates Error within the C++ Programming forums, part of the General Programming Boards category; Did I missed something here? Code: template<typename T>class A { public: template<typename T2> A<T2> toA() { return A<T2>(); } }; ...

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Strange Templates Error

    Did I missed something here?

    Code:
    template<typename T>class A {
    
    public:
      template<typename T2> A<T2> toA() {
        return A<T2>();
      }
    };
    In the main function, this code just works fine:
    Code:
    A<int> a1;
    A<float> a2 = a1.toA<float>(); // Works fine! As expected.
    I think there is nothing wrong with the code,
    but G++ gives me syntax errors:
    Code:
    template<typename T>class B {
    
      A<T> a;
    
    public:
    
      B( void ) { ; }
      B( const A<T> &a ) { this->a = a; }
    
      template<typename T3> B<T3> toB() {
        return B<T3>( a.toA<T3>() ); // error?
    
        // ???.cpp: In member function `B<T3> B<T>::toB()':
        // ???.cpp:9999: error: expected primary-expression before '(' token
        // ???.cpp:9999: error: expected primary-expression before '>' token
        // ???.cpp:9999: error: expected primary-expression before ')' token
      }
    };
    I tried to compile it in VC and it gave me a strange error message:
    Code:
    B<int> b;
    b.toB<double>(); // x:\somewhere\code.cpp(9999) : error C2062: type 'double' unexpected
    And then I tried another way:
    Code:
    template<typename T3> B<T3> toB() {
      A<T3> a1 = this->a.toA<T3>(); // won't work
    
      A<T> a2 = this->a;
      A<T3> a3 = a2.toA<T3>(); // silly, this way won't work too
    }
    Thank's in advance.

    EDIT: Sorry for my English.. T.T
    Last edited by audinue; 08-08-2009 at 08:39 AM.
    Just GET it OFF out my mind!!

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    You basically want to create A's out of certain other types of A's. You can do that. Then, you can create B's out of any suitable type of A.

    Code:
    template <typename T> class A
    {
    	public:
    		A() {}
    		template <typename U> A(const A<U> &a) {}
    };
    
    template <typename T> class B
    {
    	private:
    		A<T> a;
    	public:
    		B(const A<T> &aIn): a(aIn) {}
    };
    
    int main()
    {
    	A<int> a1;
    	A<float> a2 = a1;	
    	B<double> b(a1);
    }
    I delegate the actual conversion to the copy constructor. A conversion from U to T must be defined, as shown by the example in int to float and int to double. The same applies to any user defined conversion.

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Thank you very much whiteflags!

    It's amazing!
    Just GET it OFF out my mind!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 02:49 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM

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