problem with templates

This is a discussion on problem with templates within the C++ Programming forums, part of the General Programming Boards category; I am recieving the following compile warning/error message from g++: main.cpp:8: warning: cannot pass objects of non-POD type `class Vector4d' ...

  1. #1
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790

    problem with templates

    I am recieving the following compile warning/error message from g++:

    main.cpp:8: warning: cannot pass objects of non-POD type `class Vector4d'
    through `...'; call will abort at runtime
    this is the code that is giving it:

    Code:
    #include "3DMath.h"
                                                                                   
                                                                                   
    int main(int argc, char **argv)
    {
      Vector4d x= Vector4d(2,4,5,1);
      float mag = Magnitude(x);
      printf("%f\n",x); // dont worry, 3DNath.h links the stdio.h header
    return -1;
    };
    now, here is the definition of Magnitude:
    Code:
      template <class Vector>float Magnitude(Vector vVector)
    {
                                                                                   
     return float(sqrt( (vVector.x * vVector.x) + (vVector.y * vVector.y) + (vVector.z * vVector.z)));
                                                                                   
    };
    what exactly am I doing wrong in the use of the templated function as I am just learning templates(decided that after a year, I really did need to use them).

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    First, why do you need a template here? Why not use Vector4d as the argument directly?
    I'm not sure what causes the error. Could it be the assumption that the templated type has x, y and z members?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    The clue here is "pass objects" and "...". "Pass objects" suggests the problem is with a function; "..." is the variable argument list. The only function in your code that has a variable argument list is printf(). Upon inspection of the arguments passed to it, it would appear that you're passing a Vector4d object to it (x) where you're supposed to be passing a float (mag)
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    ah crap, i feel really really stupid :P(dont know why I missed that, perhaps lack of caffeinated beverages).

    Edit: found out what caused it, I was working on multiple projects in Xemacs and accidently wrote over part of my one with the otherone(that iswhy there is the printf("%f",x) and return -1, was in my other program in some error checking code).
    Last edited by EvBladeRunnervE; 03-23-2004 at 03:49 PM.

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    >>perhaps lack of caffeinated beverages
    I think sleep would be more beneficial to you at this point. Caffein can only work for so long before it drops from exhaustion
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  6. #6
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    Well to stray off topic, right now I am totally addicted to caffiene, I have to take it just to stay up normal hours(6am-10pm).

    still, it should of not come as a surprise considering if it were a syntax error with the templates, it would of given me it when I compiled the 3DMath.cpp file...

  7. #7
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I wish you happy templating
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  8. #8
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    First, why do you need a template here? Why not use Vector4d as the argument directly?
    Because I used both 3D and 4D vectors for some things, I also have a velocity class. So I am making all the basic Magnitude and normal finding functions templated since all of them have x,y, and z components(I only use w for the purpose of homogeous reasons, it is not like I am actually using it as a full component).

    EDIT:

    I wish you happy templating
    thanks, after a year and 3 months since I started learning C++/WIN32( I learned quite a bit, then put it off when learning OGL, now I am having to take a look back at references quite a bit)/OGL/ and some Direct X, I finally found cases where I wanted to use templating, in this case to save code space(I could save more by manipulating my vector class for all things, but I am not for asthetic reasons).
    Last edited by EvBladeRunnervE; 03-23-2004 at 03:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. Problem with templates
    By Lazy Student in forum C++ Programming
    Replies: 3
    Last Post: 11-17-2002, 12:57 PM

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