Results 1 to 4 of 4

Thread: Vector-class with template

  1. #1
    Sergeant serthy's Avatar
    Join Date
    Nov 2012
    Posts
    437
    Thanks
    92
    Thanked 278 Times in 177 Posts

    Vector-class with template

    Okay, this is actually C++ but i didnt want to open a new thread for it

    basically i wrote a simple 3d vector class and i wanted to template it in the way you can pass in any 3d vector in its constructor:

    PHP Code:
    templatetypename T >
    class 
    Vec3
    {
        public:

            
    T x z;

            
    template< class Tvec >
            
    Vec3( const Tvec);
    };

    templatetypename T >
    template< class Tvec >
    Vec3>::Vec3( const Tvec)
    {
        
    Tv.);
        
    Tv.);
        
    Tv.);
    }

    //Needed useage (Bullet's btVector3 as example):

    btVector3 v1 btVector31.0 2.0 3.0 );
    Vec3 v2 Vec3v1 ); 
    The error:
    Code:
    error C2228: left of '.x' must have class/struct/union
    error C2228: left of '.y' must have class/struct/union
    error C2228: left of '.z' must have class/struct/union
    Last edited by kung foo man; 29th November 2013 at 22:28. Reason: made new thread for different problem
    ╭━━━━━━━━━━╮
    YouTube
    Origin & Steam
    ╰━━━━━━━━━━╯

  2. #2
    Sergeant serthy's Avatar
    Join Date
    Nov 2012
    Posts
    437
    Thanks
    92
    Thanked 278 Times in 177 Posts
    Thank you Kung

    i did some more testing and changed the function from the constructor to a normal method called 'from' - voilà the error is gone
    I dont know if its done right or if i did something on the constructor wrong...mhhh actually i would like to work with the constructor on there^^
    ╭━━━━━━━━━━╮
    YouTube
    Origin & Steam
    ╰━━━━━━━━━━╯

  3. #3
    Sergeant serthy's Avatar
    Join Date
    Nov 2012
    Posts
    437
    Thanks
    92
    Thanked 278 Times in 177 Posts
    PHP Code:
    templatetypename T >
    class 
    Vec3
    {
        public:

            
    T x z;

            
    //explicit to stop unlimited implicit typeconersations a la Vec3<int> v = randomType;
            //you have declare it this way: Vec3<int> v( randomType );
            
    template< class Tvec >
            
    explicit Vec3( const Tvec);
    };

    templatetypename T >
    template< class Tvec >
    Vec3>::Vec3( const Tvec)
    {
        
    this->Tv.);
        
    this->Tv.);
        
    this->Tv.);

    seems to work

    EDIT:
    seems that my first example now compiles fine also.. ;(

    @kung under me: heheheh we will see, give me some time xxP
    Last edited by serthy; 10th December 2013 at 20:42. Reason: Uhm imanidiot
    ╭━━━━━━━━━━╮
    YouTube
    Origin & Steam
    ╰━━━━━━━━━━╯

  4. #4
    Assadministrator kung foo man's Avatar
    Join Date
    Jun 2012
    Location
    trailerpark
    Posts
    1,962
    Thanks
    1,894
    Thanked 1,062 Times in 738 Posts
    Not even 20 lines and nobody understands C++ anymore, C FTW :>
    timescale 0.01

Posting Permissions

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