cused templates! they give me no end of trouble!

Started by
1 comment, last by load_bitmap_file 18 years, 12 months ago
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!
my siteGenius is 1% inspiration and 99% perspiration
Advertisement
Until more compilers support the export keyword, templates and inline functions need to appear in your headers.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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.

This topic is closed to new replies.

Advertisement