Having trouble with templates

This is a discussion on Having trouble with templates within the C++ Programming forums, part of the General Programming Boards category; I'm just getting started writing templated code, so I apologize if this is a very silly or elementary problem. I'm ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    1

    Having trouble with templates

    I'm just getting started writing templated code, so I apologize if this is a very silly or elementary problem.

    I'm trying to write a class which will essentially be able to store and return data of a specified type, along with a few other bits of information about it. My code seems to compile, but the linker complains and won't let me actually execute the code.

    I've attached the .h and .cpp in one concatenated file to this post. If main() has this in it:

    Attribute<int> myAttr;
    myAttr.setData(3);
    cout << myAttr.getData();

    I get this error when linking:

    ld: Undefined symbols:
    Attribute<int>::getData()
    Attribute<int>::setData(int)
    Attribute<int>::Attribute[in-charge]()
    Attribute<int>::~Attribute [in-charge]()

    Any ideas on where I'm going wrong? I've looked through several tutorials and tried to mimic that code as closely as possible.

    Thanks in advance!
    Attached Files Attached Files

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You've gotten the syntax all wrong. Look at this simple template:

    Code:
    template <class type_t>
     class container
    {
      public:
    
      container(const type_t & other = type_t()) // default construct
     {
      *this = other;
     }
    
      container(const container<type_t> & other)
     {
      *this = other;
     }
    
     const container<type_t> & 
      operator = (const container<type_t> & other)
     {
      rep = other.rep;
      return *this;
     }
    
     const type_t & 
      operator = (const type_t & other)
     {
      rep = other;
      return rep;
     }
    
     type_t & get()
    { 
     return rep; 
    }
    
     type_t rep;
    };
    Does that make more sense?
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-17-2005, 11:22 PM
  2. Templates trouble
    By manannan in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2005, 11:04 AM
  3. Trouble modifing my program from templates to link lists
    By PaulTodd03 in forum C++ Programming
    Replies: 1
    Last Post: 03-26-2005, 01:15 PM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM

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