Linker Error bei Template Methode
-
Tach...
folgendes:
[code]
// mm_logger.hpp
#ifndef __MM_LOGGER_HPP__
#define __MM_LOGGER_HPP__#include <ostream>
#include <string>
#include <ctime>namespace mm {
class Logger {
private:
std::ostream& m_stream;
unsigned m_level;
public:
Logger(std::ostream& stream, const unsigned level) : m_stream(stream), m_level(level) {}
template <typename type> void LogMsg(const unsigned, const std::string, const std::string, const type&);
};}
#endif // __MM_LOGGER_HPP__
[quote][code]
#include "mm_logger.hpp"template <typename type>
void mm::Logger::LogMsg(const unsigned req,
const std::string sub,
const std::string msg,
const type& val) {
if(m_level >= req)
m_stream << "DATE, TIME " << sub << msg << val << std::endl;
}
[quote][code]
#include <iostream>
#include <string>#include "logger/mm_logger.hpp"
const std::string LEX("LEXER");
int main(int argc, char *argv[])
{
mm::Logger log(std::cerr, 2);
log.LogMsg(1, LEX, "Das ist ein Test", 3.14);
system("PAUSE");
return EXIT_SUCCESS;
}
[quote][quote="MinGW 3.4.2"]
F:\source\projects\mm_rc1\main.o(.text+0x232) In functionmain': [Linker error] undefined reference tovoid mm::Logger::LogMsg<double>(unsigned int, std::string, std::string, double const&)'
F:\source\projects\mm_rc1\main.o(.text+0x22c) ld returned 1 exit status
F:\source\projects\mm_rc1\Makefile.win [Build Error] [mm.exe] Error 1
[quote]Ich verwende DevCpp IDE, alle Datein sind im Projekt (mm_logger.cpp ist auch in der Makefile eingetragen) ... wo ist der Fehler?
MfG && THX
-
Tach...
sorry für den Doppelpost, hier nochmal besser lesbar:
// mm_logger.hpp #ifndef __MM_LOGGER_HPP__ #define __MM_LOGGER_HPP__ #include <ostream> #include <string> #include <ctime> namespace mm { class Logger { private: std::ostream& m_stream; unsigned m_level; public: Logger(std::ostream& stream, const unsigned level) : m_stream(stream), m_level(level) {} template <typename type> void LogMsg(const unsigned, const std::string, const std::string, const type&); }; } #endif // __MM_LOGGER_HPP__#include "mm_logger.hpp" template <typename type> void mm::Logger::LogMsg(const unsigned req, const std::string sub, const std::string msg, const type& val) { if(m_level >= req) m_stream << "DATE, TIME " << sub << msg << val << std::endl; }#include <iostream> #include <string> #include "logger/mm_logger.hpp" const std::string LEX("LEXER"); int main(int argc, char *argv[]) { mm::Logger log(std::cerr, 2); log.LogMsg(1, LEX, "Das ist ein Test", 3.14); system("PAUSE"); return EXIT_SUCCESS; }MinGW 3.4.2 schrieb:
F:\source\projects\mm_rc1\main.o(.text+0x232) In function
main': [Linker error] undefined reference tovoid mm::Logger::LogMsg<double>(unsigned int, std::string, std::string, double const&)'
F:\source\projects\mm_rc1\main.o(.text+0x22c) ld returned 1 exit status
F:\source\projects\mm_rc1\Makefile.win [Build Error] [mm.exe] Error 1Ich verwende DevCpp IDE, alle Datein sind im Projekt (mm_logger.cpp ist auch in der Makefile eingetragen) ... wo ist der Fehler?
MfG && THX
-
Templates müssen in jeder Übersetzungseinheit bekannt sein, um aufgelöst werden zu können, praktisch bedeutet das, dass die Defintionen der Templatemethoden in den HEader müssen.