This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: problems with gcc


Andreia R de Assuncao Schneider wrote:
> 
>   Hi,
> I made my program separating the files .h (declarations) and .cpp
> (definitions), but some classes have template, and the gcc gives
> linkage  error. I was informed that the gcc doesn't accept this
> separation. Someone can say me why this problem occur? How can I do?
> Do I do the declaration and the definition only in a file .h,
> or there is a solution for this problem?
> 

This is standard, you have to put your template definitions into the
header files so that the compiler will instantiate these functions when
it needs for different types. If you think it is getting messy, you can
also organized your code in the header/inline file form;

-------- foo.h --------------
#ifndef _foo_h_
#define _foo_h_

#include includes

template <class C>
class foo
{
public:
    foo();
    void doSomethingFn(const C &_value);
}

#include "foo.inl"
#endif // _foo_h_
------------------------------


--------- foo.inl ------------
#ifndef _foo_inl_
#define _foo_inl_

// all of the inline and template 
//	functions here
//	

template <class C>
foo<C> :: foo() 
{
}

template <class C>
void 
foo<C> :: doSomethingFn(const C &_value>
{
	// fn body
}

#endif // _foo_inl_
-------------------------------

Your makefile will still compile everything when you change something in
the foo.inl though, because it is part of the include file that is now
only organized better...

HTH,
Gokhan


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]