Sign in to follow this  

cused templates! they give me no end of trouble!

This topic is 4594 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
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

This topic is 4594 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this