C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Spezialisierung von Klassenmethoden einer Template-Klasse

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German)
View previous topic :: View next topic  
Author Message
Tom Müller
Guest





PostPosted: Mon Sep 20, 2004 5:19 pm    Post subject: Spezialisierung von Klassenmethoden einer Template-Klasse Reply with quote



Ich habe folgendes Problem mit der Spezialisierung einer Klassenmethode:
Alles funktioniert wunderbar solange nur eine Objektdatei im Spiel ist.
Sobald mehere Objectdateien benötigt werden, beschwert sich der Linker
über eine doppelte Funktionsdefinition.Offenbar erkennt der Linker nicht
das Template-bedingte Duplikat. Hier ein kurzes Beispiel:

Die Templateklasse: (temp.h)

#include<iostream>

template<class type>
class temp{
public:
temp(){};
void test(){};

};

template<>
void temp<double>::test(){
std::cout<<"Testn";
}

Hier die funktionierende Variante mit allem in einer Datei:

#include "temp.h"
#include
class test{
public:
test(){};
void print();
private:
double d;

};

void test::print(){
std::cout<<"Hallo!n";

}

int main(){

}


Und so gibt's nen Linkerfehler:

test.h:


#include "temp.h"
#include class test{
public:
test(){};
void print();
private:
double d;

};

test.cpp:


#include "test.h"

void test::print(){
std::cout<<"Hallo!n";

}


main.cpp:
#include "test.h"

int main(){


}



Dann gibt's beim Linken der Objektdateien folgenden Fehler:


g++ test.o main.o -o test
main.o(.text+0x0): In function `temp : multiple definition of `temp<double>::test()'
test.o(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [test] Fehler 1


Weiss jemand ne Lösung?

Schon mal danke

Tom

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de
Back to top
Torsten Robitzki
Guest





PostPosted: Mon Sep 20, 2004 7:40 pm    Post subject: Re: Spezialisierung von Klassenmethoden einer Template-Klass Reply with quote



Tom Müller wrote:

<snip>
Quote:
Weiss jemand ne Lösung?

Dir fehlt die Deklaration der Spezialisierung temp für double. Das der
compiler template<> void temp<double>::test() überhaupt durch läßt ist
denke ich ein Fehler.

mfg Torsten

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Olaf Krzikalla
Guest





PostPosted: Tue Sep 21, 2004 10:05 am    Post subject: Re: Spezialisierung von Klassenmethoden einer Template-Klass Reply with quote



Hi,

Tom Müller schrieb:
Quote:
Die Templateklasse: (temp.h)

#include<iostream

template class temp{
public:
temp(){};
void test(){};

};

template
void temp std::cout<<"Testn";
}
Das ist eine explizite vollständige Spezialisierung. Deren Definition

gehört in eine .cpp-Datei. Der Linker wird nur compiler-generierte
Template-Funktionen zusammenfassen (also nicht oder nur partiell
spezialiserte Funktionen). Ob man in der Header-Datei die
Spezialisierung deklarieren muß, weiß ich allerdings nicht (bei mir
funktionierts auch ohne).

MfG
Olaf Krzikalla

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Tom Müller
Guest





PostPosted: Tue Sep 21, 2004 11:51 am    Post subject: Re: Spezialisierung von Klassenmethoden einer Template-Klass Reply with quote


Quote:
Das ist eine explizite vollständige Spezialisierung. Deren Definition
gehört in eine .cpp-Datei. Der Linker wird nur compiler-generierte
Template-Funktionen zusammenfassen (also nicht oder nur partiell
spezialiserte Funktionen). Ob man in der Header-Datei die
Spezialisierung deklarieren muß, weiß ich allerdings nicht (bei mir
funktionierts auch ohne).


Aha,dann muß ich also doch erstmal Objektcode erzeugen. Und wie müßte
man eine solche Spezialisierung deklarieren? Ich hab nämlich noch keine
funktionierende Variante finden können.

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Olaf Krzikalla
Guest





PostPosted: Wed Sep 22, 2004 2:07 pm    Post subject: Re: Spezialisierung von Klassenmethoden einer Template-Klass Reply with quote

Hi,

Tom Müller schrieb:
Quote:
Aha,dann muß ich also doch erstmal Objektcode erzeugen. Und wie müßte
man eine solche Spezialisierung deklarieren? Ich hab nämlich noch keine
funktionierende Variante finden können.

Keine Ahnung. Naheliegend wäre ja

template<> void temp<double>::test();

AFAIK muß im Moment einer Template-Instantiierung auch bekannt sein, ob
eine Spezialisierung für diese Instantiierung existiert, d.h. eine
Deklaration wäre eigentlich schon notwendig. Vielleicht hat Tiorsten
sogar recht und explizite Spezialisierungen für einzelne
Member-Funktionen werden vom Standard gar nicht abgedeckt. In diesem
Fall funktioniert es bei mir (Borland-Compiler) nur zufälligerweise.
Hast Du's mal mit Comeau gecheckt?

MfG
Olaf Krzikalla

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Olaf Krzikalla
Guest





PostPosted: Wed Sep 22, 2004 2:56 pm    Post subject: Re: Spezialisierung von Klassenmethoden einer Template-Klass Reply with quote

Hi,

kurzer Nachtrag:

Quote:
template<> void temp<double>::test();

Funktioniert auf comeau-online.

MfG
Olaf Krzikalla

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Tom Müller
Guest





PostPosted: Fri Sep 24, 2004 7:41 am    Post subject: Re: Spezialisierung von Klassenmethoden einer Template-Klass Reply with quote



Olaf Krzikalla wrote:

Quote:
Hi,

kurzer Nachtrag:


template<> void temp<double>::test();


Funktioniert auf comeau-online.



Dann aber außerhalb der Klasse, oder? Hatte versucht das in der Klasse
versucht und das ging (zumindest mit g++) nicht.

Danke


Lars

--
de.comp.lang.iso-c++ - Moderation: mailto:voyager+mod (AT) bud (DOT) prima.de
FAQ: http://www.voyager.prima.de/cpp/ mailto:voyager+send-faq (AT) bud (DOT) prima.de

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ (German) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.