Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old May 14th, 2006, 11:50 PM
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
  #2 (permalink)  
Old May 15th, 2006, 12:04 AM
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default

Try using Google - it gives 94,000 entries for "LNK1120 error"

such as - http://www.kbalertz.com/kb_909350.aspx
and - http://support.microsoft.com/default...b;en-us;815650

Also find Microsoft Knowledge Base (use Google again) and get familiar with it if you are using Microsoft products.

If you get yourself familiar with search techniques you can save yourself a lot of time.

Alan

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
fatal error LNK1120: Yomero C++ Programming 0 November 11th, 2008 05:35 PM
CH 14 - Link Error - Unresolved Razzy The Pug BOOK: Ivor Horton's Beginning Visual C++ 2005 2 March 18th, 2008 06:44 PM
error LNK2020 unresolved token rachappa Visual C++ 1 January 12th, 2006 08:05 AM
error LNK2019: unresolved external symbol tinawalt VS.NET 2002/2003 3 August 9th, 2005 07:59 AM
fatal error!!!! Ashleek007 Beginning PHP 6 October 9th, 2004 10:25 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.