Thread: template functions inside a class

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    template functions inside a class

    The code below won't compile, it gives me an unresolved external. Anyone know why?

    Code:
    class BaseParticleManager
    {
    public:
    	virtual ~BaseParticleManager( void );
    
    	template<typename T> void AddNewParticle(const T & throwaway);
    
    private:
    	std::list < Particle * > ParticleList;
    };
    Code:
    template<typename T> 
    void BaseParticleManager :: AddNewParticle(const T & throwaway)
    {
    	ParticleList.push_back( new T );
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    What compiler are you using? Do you have the destructor defined anywhere? The template stuff looks fine.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >it gives me an unresolved external
    Could you give us the text of that error? My error with your very incomplete code was talking about BaseParticleManager's destructor. After giving it a body, the compile and link only gave one warning. To clear the warning, you can remove the argument to AddNewParticle and give the template an explicit instantiation:
    Code:
    template<typename T> 
    void BaseParticleManager :: AddNewParticle()
    {
    	ParticleList.push_back( new T );
    }
    ...
    BaseParticleManager bpm;
    bpm.AddNewParticle<Particle>();
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    class SnowParticle : Public Particle;

    thought I'd add that.

    TestTask.obj : error LNK2019: unresolved external symbol "public: void __thiscall BaseParticleManager::AddNewParticle<class SnowParticle>(class SnowParticle const &)" (??$AddNewParticle@VSnowParticle@@@BaseParticleMan ager@@QAEXABVSnowParticle@@@Z) referenced in function "public: virtual bool __thiscall CTestTask::Start(void)" (?Start@CTestTask@@UAE_NXZ)
    Debug/RTMB.exe : fatal error LNK1120: 1 unresolved externals
    and when I do it explicitly, like you said...

    TestTask.obj : error LNK2019: unresolved external symbol "public: void __thiscall BaseParticleManager::AddNewParticle<class SnowParticle>(void)" (??$AddNewParticle@VSnowParticle@@@BaseParticleMan ager@@QAEXXZ) referenced in function "public: virtual bool __thiscall CTestTask::Start(void)" (?Start@CTestTask@@UAE_NXZ)
    Last edited by Trauts; 03-12-2004 at 09:36 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Post a short program that we can compile that exhibits the problem.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    it works when I recompile it elsewhere, in a separate program... thats REALLY weird.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Attachment: particles.zip
    ...

    >it works when I recompile it elsewhere, in a separate program...
    Clearly you're making the mistake in your big program but not in the little program. Compare and contrast the relevant parts of the two programs. I can't help you because I don't download attached zip files, like many other regular contributors to cprogramming.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    do you want me to attach them as .cpp/.h's?

    I DID copy and paste, but I also deleted some excess stuff... hrm..


    It works fine, doesn't give me the unresolved external, unless I uncomment this line:

    ParticleController.AddNewParticle<SnowParticle>();

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    got it working. it wasn't recognizing it in the .cpp for some reason

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linker error
    By tasha302 in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2006, 12:00 AM
  2. help with template class using a template node
    By aciarlillo in forum C++ Programming
    Replies: 11
    Last Post: 06-02-2005, 05:46 PM
  3. problem with const int inside class (c2258)
    By talz13 in forum C++ Programming
    Replies: 1
    Last Post: 11-23-2003, 07:34 PM
  4. Data init inside of template class function
    By VirtualAce in forum C++ Programming
    Replies: 1
    Last Post: 04-24-2002, 03:11 PM
  5. bug for static functions in template class
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2001, 06:38 PM