Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7840961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:58:56+00:00 2026-06-02T15:58:56+00:00

I need to generated a simple Logger (with out third party libs) that’s need

  • 0

I need to generated a simple Logger (with out third party libs) that’s need to be link to dll. Therefore I generated a class with some of his class method were templates, the code compile fine but I get linker error. I am compiling with MS VS 2008 and gcc-4. The code is:

Log.h class:

class MiniLogger
{
private:
   std::ofstream mOutPutFile;
protected:
   void write (std::string const& text);
public:
   ~MiniLogger();
   MiniLogger( std::string const& lName) throw(FileError);
   static MiniLogger* getInstance(std::string const & fName);
   static void destoryInstance(MiniLogger*);

  template<class T>
  MiniLogger& operator<<(T & data);
  MiniLogger& operator<<(std::ostream& (*func)(std::ostream&) );

};


MiniLogger& MiniLogger::operator<< (std::ostream& (*func)(std::ostream&))
{

   //mOutPutFile << out;
   return *this;
}
template<class T>
MiniLogger& MiniLogger::operator<< (T  & data)
{
 //Just try with out calling other method
// write(data);
   return *this;
}

At main I instantiated the Object and use it :

#include "log.h"

int main()
{


    MiniLogger &a=*(MiniLogger::getInstance("text.txt"));

    a << "text" << std::endl;


return 0;
}

I get

@ubu11-10-64b-01:~/cplus/template$ g++ main.cpp log.cpp
/tmp/cccMdSBI.o: In function `MiniLogger::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))':
log.cpp:(.text+0x0): multiple definition of `MiniLogger::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccj3dfhR.o:main.cpp:(.text+0x0): first defined here
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:58:59+00:00Added an answer on June 2, 2026 at 3:58 pm

    You’ve defined a function in a header file. Since that header file is included in multiple translation units (namely, main.cpp and log.cpp), you’ve multiply defined the function. (See the One Definition Rule.)

    Either declare the function inline and define it in the header file, or declare it extern in the header file and define in exactly one source file.

    The function in question is: MiniLogger& MiniLogger::operator<< (std::ostream& (*func)(std::ostream&)).

    Solution #1:

    // Log.h
    inline
    MiniLogger& MiniLogger::operator<< (std::ostream& (*func)(std::ostream&))
    {
     //mOutPutFile << out;
     return *this;
    }
    

    Solution #2:

    // Log.h
    extern
    MiniLogger& MiniLogger::operator<< (std::ostream& (*func)(std::ostream&));
    
    // Log.cpp
    MiniLogger& MiniLogger::operator<< (std::ostream& (*func)(std::ostream&))
    {
     //mOutPutFile << out;
     return *this;
    
    }
    

    Side Note: Realize that the next entity in the header file, template<class T>
    MiniLogger& MiniLogger::operator<< (T & data)
    , is not a function – it is a function template – and different advice applies to it. As a rule of thumb, you should define function templates in header files, but should not define functions in header files. (There are exceptions to this rule of thumb.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to take some simple UL tag generated in PHP (Joomla 1.5) and
I need to parse files generated by a third-party application. Using ANTLR, I have
I need to embed a link into a generated pdf in a ruby on
I have a wordpress-generated page where I need some posts (in a specific category)
I have a series of dynamically generated inputs that I need to have ajax
I need to count the number of instructions that will be generated for an
I've generated a simple Rails 3 class using the scaffold function: rails generate scaffold
I've written a simple REST service class, and I need to deploy and run
I have a very simple SQL query that I made which retrieves some user
I need to make a few simple changes to the Datepicker HTML generated by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.