Thread: Linking Error Whenever I Tried To Use Header Files

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    32

    Question Linking Error Whenever I Tried To Use Header Files

    I tried some codes from Deitel & Deitel's books & whenever I tried codes with headers or classes I will receive a linking error message like this one:

    Configuration: fig06_08 - Win32 Debug--------------------
    Linking...
    fig06_08.obj : error LNK2001: unresolved external symbol "public: void __thiscall Time::printStandard(void)" (?printStandard@Time@@QAEXXZ)
    fig06_08.obj : error LNK2001: unresolved external symbol "public: void __thiscall Time::printMilitary(void)" (? printMilitary@Time@@QAEXXZ)
    fig06_08.obj : error LNK2001: unresolved external symbol "public: __thiscall Time::Time(int,int,int)" (??0Time@@QAE@HHH@Z)
    Debug/fig06_08.exe : fatal error LNK1120: 3 unresolved externals
    Error executing link.exe.

    fig06_08.exe - 4 error(s), 0 warning(s)




    This puzzled me because when I compiled the file with my Visual C++ 6 there was no error or warning. I tried to run the following program with my Borland C++ 5.02 & it wasn't working either.

    Can anyone tell me what is wrong? This is very frustrating because I am learning classes with C++.

    Thanks.

    time2.h source code:

    Code:
    #ifndef TIME2_H
    #define TIME2_H
    
    class Time
    {
    public:
    	Time(int = 0, int = 0, int = 0);
    	void setTime(int, int, int);
    	void printMilitary();
    	void printStandard();
    private:
    	int hour;
    	int minute;
    	int second;
    
    };
    
    #endif

    time2.cpp source code:

    Code:
    #include<iostream>
    using std::cout;
    
    #include "time2.h"
    
    
    Time::Time(int hr, int min, int sec)
    {setTime(hr, min, sec);}
    
    
    void Time::setTime(int h, int m, int s)
    {
    	hour =   (h>=0 && h < 24) ? h :0;
    	minute = (m>=0 && m < 60) ? m :0;
    	second = (s>=0 && s < 60) ? s :0;
    }
    
    void Time::printMilitary()
    {
    	cout<<(hour < 10 ? "0" : "") <<hour<< ":"
    		<<(minute < 10 ? "0" : "") << minute;
    }
    
    
    void Time::printStandard()
    {
    	cout<<((hour == 0 || hour == 12) ? 12 : hour % 12)
    		<<":" << (minute < 10 ? "0" : "") << minute
    		<<":" << (second < 10 ? "0" : "") << second
    		<<( hour < 12 ? "AM" : "PM");
    }



    main.cpp source code:

    Code:
    #include<iostream>
    using std::cout;
    using std::endl;
    
    #include "time2.h"
    
    
    int main()
    {
    	Time t1,
    		t2(2),
    		t3(21,34),
    		t4(12,25,42),
    		t5(27,74,99);
    	cout<<"Constructed with:\n"
    		<<"all arguments defaulted:\n ";
    	t1.printMilitary();
    	cout<<"\n ";
    	t1.printStandard();
    
    	cout<<"\nhour specified; minute and second defaulted:"
    		<<"\n ";
    	t2.printMilitary();
    	cout<<"\n ";
    	t2.printStandard();
    
    	cout<<"\nhour and minute specified: second defaulted:"
    		<<"\n ";
    	t3.printMilitary();
    	cout<<"\n ";
    	t3.printStandard();
    
    	cout<<"\nhour, minute, and second specified:"
    		<<"\n ";
    	t4.printMilitary();
    	cout<<"\n ";
    	t4.printStandard();
    
    	cout<<"\nall invalid values specified:"
    		<<"\n ";
    	t5.printMilitary();
    	cout<<"\n ";
    	t5.printStandard();
    	cout<<endl;
    
    	return 0;
    }
    [SIZE= 4]My favorite search engine is http://www.ultimasurf.com [/size]

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    You need to make sure you have time2.cpp AND main.cpp in your project (or on the command line)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    32
    How?

    Sorry, I don't know how.


    Thanks.
    [SIZE= 4]My favorite search engine is http://www.ultimasurf.com [/size]

  4. #4
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Just make sure you actually compile time2.cpp. I've only been using DevC++ on windows, but I think it should be enough to have it in the project file of your IDE. On the commandline you have to compile both - main.cpp and time2.cpp before you can link them.
    This is a shortcoming of the C language. It doesn't know which object belongs to which "header" (unit).
    [code]

    your code here....

    [/code]

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    32
    Thank you very much for the information. It is working now.


    I need to try DevC++. Is it free?
    [SIZE= 4]My favorite search engine is http://www.ultimasurf.com [/size]

  6. #6
    *******argv[] - hu? darksaidin's Avatar
    Join Date
    Jul 2003
    Posts
    314
    Originally posted by javacvb
    Thank you very much for the information. It is working now.


    I need to try DevC++. Is it free?
    It's on opensource IDE for windows using an opensource compiler ported from linux to windows (gcc / mingw) and to make things even more ironic: It's been written in Delphi (Pascal).

    As long as you're not going to develop windows user interfaces, it should be more than sufficient. Especially for OpenGL apps

    You can get it here: http://sourceforge.net/projects/dev-cpp/
    [code]

    your code here....

    [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  2. C Header Files
    By devarishi in forum C Programming
    Replies: 8
    Last Post: 12-10-2008, 04:53 PM
  3. Header files in .h
    By swgh in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2008, 08:58 AM
  4. Missing header files and libraries with Borland
    By quagsire in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2003, 06:19 AM
  5. Header files
    By Jez_Master in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2002, 07:56 AM