Jump to content
  • Advertisement
Sign in to follow this  
Spudder

operator overloading for a log class

This topic is 5478 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've got a log class which currently overloads op<< for outputing a string (via std::string) to a log file but I'd like to make it able to output any sort of data - floats/ints etc. I tried making it a templated function but then got unresolved external errors for the different versions of the function the compiler generated, is there an elegant solution to fix this? I don't fancy having to overload op<< for every data type which is just ugly.

Share this post


Link to post
Share on other sites
Advertisement
Are you sure you've written it correctly, you mean something like this:


#include <string>
#include <fstream>

template< typename charT = char, typename traits = std::char_traits<charT> >
class log {

std::basic_ofstream< charT, traits > fout;

log(const log<charT, traits>&) {}

log<charT, traits>& operator=(const log<charT, traits>&) {
return *this;
}

public:

log(const std::string& filename): fout(filename.c_str()) {}

template< typename T >
log<charT, traits>& operator<<(const T& val) {
fout << val;
return *this;
}

};

int main() {

log<char> l("mo_go.txt");

l << 30 << " Hello\n";

return 0;

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!