Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > de.comp.lang.iso-c++ > #2016 > unrolled thread
| Started by | Henning Follmann <hfollmann@itcfollmann.com> |
|---|---|
| First post | 2017-02-08 10:48 -0500 |
| Last post | 2017-02-09 18:39 +0100 |
| Articles | 2 — 2 participants |
Back to article view | Back to de.comp.lang.iso-c++
Linker Problem mit Template Class Henning Follmann <hfollmann@itcfollmann.com> - 2017-02-08 10:48 -0500
Re: Linker Problem mit Template Class Stefan Reuther <stefan.news@arcor.de> - 2017-02-09 18:39 +0100
| From | Henning Follmann <hfollmann@itcfollmann.com> |
|---|---|
| Date | 2017-02-08 10:48 -0500 |
| Subject | Linker Problem mit Template Class |
| Message-ID | <slrno9mfdh.2f3.hfollmann@smallapple.itcfollmann.com> |
Ich habe hier ein Problem beim Linken mit einer template class. Ich habe
einmal ein vereinfachtes Beispiel zusammengestellt:
printer.hpp
====================================
#ifndef _PRINTER_HPP_INCLUDE_
#define _PRINTER_HPP_INCLUDE_
#include <iostream>
template <typename T1>
class printer{
std::ostream& _out;
public:
printer(std::ostream& os):_out{os} {};
void operator()(T1 var);
};
#endif
====================================
printer.cpp
====================================
#include "printer.hpp"
template <typename T1>
void printer<T1>::operator()(T1 var){
_out<<var<<"\n";
}
====================================
maintest.cpp
====================================
#include <iostream>
#include "printer.hpp"
int main()
{
printer<int> prntr{std::cout};
prntr(25);
return 0;
};
====================================
Die Uebersetzung schlaegt fehl. Genauer gesagt, die Bindung:
g++ -std=c++11 maintest.cpp printer.cpp
/tmp/ccRR5tP2.o: In function `main':
maintest.cpp:(.text+0x26): undefined reference to
`printer<int>::operator()(int)'
collect2: error: ld returned 1 exit status
Ist das ein Fehler des Linkers?
Gruss
Henning
--
Henning Follmann | hfollmann@itcfollmann.com
[toc] | [next] | [standalone]
| From | Stefan Reuther <stefan.news@arcor.de> |
|---|---|
| Date | 2017-02-09 18:39 +0100 |
| Message-ID | <o7id1k.4do.1@stefan.msgid.phost.de> |
| In reply to | #2016 |
Am 08.02.2017 um 16:48 schrieb Henning Follmann:
> Ich habe hier ein Problem beim Linken mit einer template class. Ich habe
> einmal ein vereinfachtes Beispiel zusammengestellt:
[...]
> printer.cpp
> ====================================
> #include "printer.hpp"
>
> template <typename T1>
> void printer<T1>::operator()(T1 var){
> _out<<var<<"\n";
> }
Die Template-Instanziierung muss ins Headerfile, damit der Compiler das
Template sieht, wenn er es instanziieren muss.
Stefan
[toc] | [prev] | [standalone]
Back to top | Article view | de.comp.lang.iso-c++
csiph-web