tags:

views:

99

answers:

2

I have been playing around with Template functions, and made a little logger program. I have been trying to split this into header / source file, but I keep getting linker errors. I know this is simple, but I cant figure it out.

Also I have some convince vars in the logger header, where would be the "proper" place for those? (logStart, logEnd etc.)

All code below.

Header

#ifndef __moot_logger_hpp__
#define __moot_logger_hpp__


#include <iostream>
#include <fstream>
#include <string>

using std::ios;

namespace Moot
{
    struct Logger
    {
        Logger();
        ~Logger() {}


        template <class T>      
        void saveToFile(T type);

        template <typename T>
        Logger& operator<< (T type);


        /*
        Logger& operator<< (std::wstring wideStr)
        {
            saveToFile(Moot::convertWstringToString(wideStr));
            return *this;
        }
        */
    };


    /*
    namespace {
        Logger logStart;
        Logger& lStart = logStart;

        const char  logEnd = '\n';
        const char& lEnd   = logEnd;

        const char  logSpace = ' ';
        const char& lSpace   = logSpace;
    }
    */

}


#endif

Source

#include <Moot/logger.hpp>

Moot::Logger::Logger()
{
    std::ofstream logfile;
    logfile.open ("logfile.txt", ios::trunc);
    logfile << "LogFile - most recent at the bottom\n";
    logfile << "-----------------------------------\n \n";
    logfile.close();
}


template <typename T>
void Moot::Logger::saveToFile(T type)
{
    std::ofstream logfile;
    logfile.open ("logfile.txt", ios::in | ios::app);
    logfile << type;
    logfile.close();
}


template <typename T>
Moot::Logger& Moot::Logger::operator<< (T type) 
{
    saveToFile(type);
    return *this;
}


/*
Logger::Logger& operator<< (std::wstring wideStr)
{
    saveToFile(Moot::convertWstringToString(wideStr));
    return *this;
}
*/

main

#include <Moot/logger.hpp>
#include <iostream>

int main()
{
    std::cout << "hello" << std::endl;

    Moot::Logger() << 12;
    //Moot::lStart.saveToFile(23);
    //Moot::lStart.operator << 13;

    return 0;
};
+5  A: 

You'll want to read the C++ FAQ Lite question, "Why can't I separate the definition of my templates class from its declaration and put it inside a .cpp file?"

Effectively, you need to define your function and class templates in the header file, not in the .cpp file.

James McNellis
A: 

If you want divide definition from declaration for template classes than move definition to separate file with hpp or tpp (tpp not always correctly handled by text editors) extension and add include directive for definition file at the end of declaration file

whalebot.helmsman