Jump to content
  • Advertisement
Sign in to follow this  
silverphyre673

cused templates! they give me no end of trouble!

This topic is 4757 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

As title. Here is the problem (assume functions and classes Item and WriteBin are already defined, and work properly (they do). in main.cpp
main()
{
    Item item;
    item( "ITEM_LONGBOW", "Longbow", "A stout longbow of yew and ash.  Fires long            arrows, suitable for hunting or warfare.", 6 );
    WriteObject( item, "ITEM_LONGBOW.RIF");    
    return 0;
}

in function.h
#ifndef FUNCTION_H
#define FUNCTION_H

#include "other_functions.h"

#include <fstream>

template<class A>
bool WriteObject( A & object, std::string filename );

#endif

in function.cpp
#ifndef FUNCTION_CPP
#define FUNCTION_CPP

#include "function.h"

template<class A>
bool WriteObject( A & object, std::string filename )
{
    std::ofstream fout ( filename.c_str(), std::ofstream::binary );
    if (!fout )
        return false;
    for ( unsigned i = 0; i < object.NumberMember(); ++i )
        WriteBin( (char*)&object.AccessMember(i), object.Size(i) );
    fout.close();
}

#endif

It gives me this error: in function 'main': [Linker error] undefined reference to `bool WriteObject<Item>(Item&, std::string)' How come? Thanks!

Share this post


Link to post
Share on other sites
Advertisement
You have to define templates in the .h file. Take your definition in your .cpp file and stick it in your .h instead.

EDIT: Well fudge, beaten.

smart_idiot: it doesn't seem like many people want to implement export. I asked about it a while ago in a thread and apparently not enough people are asking for it so the compiler writers can't justify spending time and money implementing it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!