Thread: Templates and overloaded ==

  1. #1
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820

    Templates and overloaded ==

    Hey guys while working on a new crappy game I have stumbled across a problem. I am trying to use a template for a overloaded == in my class for collision. I am not good with words here is the code
    character.h
    Code:
    #ifndef character_h
    #define character_h
    
    #include <allegro.h>
    
    class character
    {
    
    public:
      character();
      character(int x, int y, const char *image1, const char *image2);
      template <class T>
      bool operator ==(T &obj);//handle collisions
      BITMAP *retImage() const;//returns current image  
    
    /*************************************************************************
      Decided to make these variables public because they would be accessed 
      alot and functions to get and set them would be a waste
    **************************************************************************/
      int xPos;//character's x coord for drawing
      int yPos;//character's y coord for drawing
      int currentImage;//to hold currrent image for switching image for movement
      int left;//left coord for collision
      int right;//right coord for collision
      int top;//top coord for collision
      int bottom;//bottom coord for collision
    
    private:
      BITMAP *image[2];//The characters image  have two for "animation"
    
    };
    
    #endif
    character.cpp
    Code:
    template <class T>
    bool character::operator==(T &obj)
    {
      
      return !(right < obj.left||obj.right < left||bottom < obj.top||obj.bottom < top);
    
    }
    All seems to be well with this but when I go to use it in my code for some detection I get an unresolved linker error. Its just a simple if statement eg.
    Code:
    if(player == enemy)
    {
      //do stuff
    }
    Is there some way that I need to be handling the enemy. I know with templates and functions that you would do an <type> before you would call it but that doesn't seem reasonable here.
    Woop?

  2. #2
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Dunno. What's the linker error (what symbol is unresolved)? It could be something else down the line that's causing the problem.

    **EDIT**
    Hm, isn't there something about templates always having to be defined inside the header (i.e. not in a separate .cpp file)? Not sure if that applies here, but it's worth a shot.
    Just Google It. √

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

  3. #3
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    How new am I forgetting to post the error .
    Code:
    [Linker error] undefined reference to `bool character::operator==<character>(character&)'
    [edit]Your method worked hunter. Thank you[/edit]
    Woop?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 06-04-2008, 05:57 PM
  2. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  3. Problem with overloaded function templates
    By New++ in forum C++ Programming
    Replies: 10
    Last Post: 09-05-2005, 04:00 PM
  4. templates and friends
    By silk.odyssey in forum C++ Programming
    Replies: 3
    Last Post: 06-12-2004, 11:13 AM
  5. Problem with overloaded operators, templates and inheritance
    By bleakcabal in forum C++ Programming
    Replies: 1
    Last Post: 03-19-2004, 05:07 AM