Har börjat använda templates igen och nu måste jag få svar på min fråga som jag har haft sedan länge innan jag blir galen.
Hur gör man för att får en template deklaration i en hpp fil och definitionen i cpp filen?? Jag får bara kompilator fel när jag försöker dela upp det på ett snyggt sätt.
Ex
[i]Kalle.hpp[/i]
template <class X>
class Kalle
{
public:
void swapvals(X &a,X &b)
{
X temp;
temp=a;
a=b;
b=temp;
};
};
Detta fungerar perfekt att använda från en vanlig klass.
När jag gör såhär
[i]Kalle.hpp[/i]
template <class X>
class Kalle
{
public:
void swapvals(X &a,X &b);
};
[i]Kalle.cpp[/i]
#include "Kalle.hpp"
template <class X>
void Kalle::swapvals(X &a,X &b)
{
X temp;
temp=a;
a=b;
b=temp;
}
Så fungerar det inte längre, då får jag typ (lite andra filnamn och rader men det är samma kod...)
dcclassics/classics.cpp:60: syntax error before `::' token
dcclassics/classics.cpp:60: `X' was not declared in this scope
dcclassics/classics.cpp:60: `a' was not declared in this scope
dcclassics/classics.cpp:60: `X' was not declared in this scope
dcclassics/classics.cpp:60: `b' was not declared in this scope
dcclassics/classics.cpp:61: ISO C++ forbids declaration of `swapvals' with no
type
dcclassics/classics.cpp:61: initializer list being treated as compound
expression
dcclassics/classics.cpp:61: syntax error before `{' token
dcclassics/classics.cpp:63: ISO C++ forbids declaration of `temp' with no type
dcclassics/classics.cpp:63: `a' was not declared in this scope
dcclassics/classics.cpp:64: ISO C++ forbids declaration of `a' with no type
dcclassics/classics.cpp:64: `b' was not declared in this scope
dcclassics/classics.cpp:65: ISO C++ forbids declaration of `b' with no type
dcclassics/classics.cpp:66: parse error before `}' token
Hur ska man göra eller går det helt enkelt inte att dela upp en template i flera filer??
Kör GCC 3.0.4
/Viktor