View Single Post
  #1 (permalink)  
Old May 14th, 2006, 11:50 PM
newbiecoder_bizz newbiecoder_bizz is offline
Registered User
 
Join Date: May 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default fatal error LNK1120: 1 unresolved externals

Hi,

I am totally new to C++ and I'm trying to create a simple program using Visual Studio 2005. I'm using win32 console project.

The idea is I have a class inside of a namespace declared on a .h file. The definition of the class is on a .cpp file.
The problem is the defined template function is not being recognized on link stage. But when the template function:

template< class T >
      int write( const T& pObj, bool LogState = true,
                 std::streamsize precision = 2,
                 std::ios_base::fmtflags iosFlags = std::ios::fixed
               );

is defined inline on the .h file, it works well.

Please help me with this.. I anticipate any form of help.

Please bear with my spaghetti code here:


//Logger.cpp
#include "incs.h"

namespace log
{
  Logger::Logger()
  {
    try
    {
        LogFileName = "EventHistory.log";
        LogFile.open( LogFileName.c_str(), std::ios::app );
    }
    catch(std::ios_base::failure& )
    {
        //std::cout<<"Error: "<<e.what()<<std::endl;
      throw; //throw exception to outer level error handler
    }
  }

  Logger::Logger( const std::string& filename )
  {
    try
    {
        LogFile.open( filename.c_str(), std::ios::app );
    }
    catch( std::ios_base::failure& )
    {
      throw;
    }
  }

  Logger::~Logger()
  {
    try
    {
        LogFile.flush();
        LogFile.close();
    }
    catch( std::ios_base::failure& )
    {
      throw;
    }
  }

  template< class T > int Logger::write( const T& pObj , bool LogState,
                                              std::streamsize precision,
                                              std::ios_base::fmtflags iosFlags )
  {

    if( LogState)
    {
        LogFile<<std::endl;
        LogFile<<"Date: "<<__DATE__<<std::endl;
        LogFile<<"File: "<<__FILE__<<std::endl;
        LogFile<<"Time: "<<__TIME__<<std::endl;

      LogFile<<std::setiosflags( iosFlags );
      LogFile<<std::setprecision( precision );

        LogFile<<"Activity: "<<pObj<<std::endl;
      }

      return 0;
  }
}


//Logger.h

#ifndef __LOGGER_H__
#define __LOGGER_H__

namespace log
{
    class Logger
    {
      private:
          std::ofstream LogFile;
          std::string LogFileName;

      //protected:

      public:
          Logger();
          Logger( const std::string& filename );
          ~Logger();

      template< class T >
      int write( const T& pObj, bool LogState = true,
                 std::streamsize precision = 2,
                 std::ios_base::fmtflags iosFlags = std::ios::fixed
               );

  };
}

#endif


//LoggerTester.cpp

#include "incs.h"
#include "Logger.h"

using namespace log;

int main( int argc, char*argv[] )
{
  try
  {
    Logger EventLog;
      char ch = 'C';

      EventLog.write( ch );
      EventLog.write<std::string>("This string will be logged.");
    EventLog.write<float>(10.50);

    try
    {
      EventLog.write('T',true );
      EventLog.write("Another string to be logged.", std::ios_base::fixed );
      EventLog.write(500.7621, true, 4, std::ios::fixed );

      EventLog.write("This string won't be written on the log.", false );
    }
    catch( std::ios_base::failure& )
    {
      throw;
    }
  }
  catch( std::ios_base::failure& e )
  {
    std::cout<<"Error: "<<e.what()<<std::endl;
  }

  try
  {
    Logger EventLog( "EventHistory2.log" );
    EventLog.write("Writing events to event logger.");
  }
  catch( std::ios_base::failure& e )
  {
    std::cout<<"Error: "<<e.what()<<std::endl;
  }

    return 0;
}


//incs.h

//standard libraries
#include <iostream>
#include <exception>
#include <string>
#include <iomanip>
#include <fstream>


//defined headers
#include "Logger.h"




Linker result:
LoggerTester.obj : error LNK2019: unresolved external symbol "public: int __thiscall log::Logger::write<char const [32]>(char const (&)[32],bool,int,int)" (??$write@$$BY0CA@$$CBD@Logger@log@@QAEHAAY0CA@$$C BD_NHH@Z) referenced in function __catch$_main$0
LoggerTester.obj : error LNK2019: unresolved external symbol "public: int __thiscall log::Logger::write<char const [41]>(char const (&)[41],bool,int,int)" (??$write@$$BY0CJ@$$CBD@Logger@log@@QAEHAAY0CJ@$$C BD_NHH@Z) referenced in function _main
LoggerTester.obj : error LNK2019: unresolved external symbol "public: int __thiscall log::Logger::write<double>(double const &,bool,int,int)" (??$write@N@Logger@log@@QAEHABN_NHH@Z) referenced in function _main
LoggerTester.obj : error LNK2019: unresolved external symbol "public: int __thiscall log::Logger::write<char const [29]>(char const (&)[29],bool,int,int)" (??$write@$$BY0BN@$$CBD@Logger@log@@QAEHAAY0BN@$$C BD_NHH@Z) referenced in function _main
LoggerTester.obj : error LNK2019: unresolved external symbol "public: int __thiscall log::Logger::write<float>(float const &,bool,int,int)" (??$write@M@Logger@log@@QAEHABM_NHH@Z) referenced in function _main
LoggerTester.obj : error LNK2019: unresolved external symbol "public: int __thiscall log::Logger::write<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,bool,int,int)" (??$write@V?$basic_string@DU?$char_traits@D@std@@V ?$allocator@D@2@@std@@@Logger@log@@QAEHABV?$basic_ string@DU?$char_traits@D@std@@V?$allocator@D@2@@st d@@_NHH@Z) referenced in function _main
LoggerTester.obj : error LNK2019: unresolved external symbol "public: int __thiscall log::Logger::write<char>(char const &,bool,int,int)" (??$write@D@Logger@log@@QAEHABD_NHH@Z) referenced in function _main
D:\development\course\development\ANSI C++\miscellaneous\Activity File Logger\Debug\Activity File Logger.exe : fatal error LNK1120: 7 unresolved externals

&lt;bizz&gt;
Reply With Quote