Linking errors
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Linking errors

  1. #1
    Join Date
    Sep 2010
    Posts
    3

    Linking errors

    I am having a hard time linking my header file with my cpp file.

    My header file time24.h is as follows:



    #ifndef time24_h
    #define time24_h


    class time24
    {
    public:
    time24(int h = 0, int m = 0);
    // Constructor initializes hour and minute


    void addTime(int m);
    //update time by adding m minutes to the current time
    //Precondition: m must be >=0


    time24 duration(const time24& t);
    // return the length of time from some current time to some later time
    // time t as a time24 value


    void readTime();
    // Reads in time from keyboard in hh:mm format
    //Postcondition: Assign value hh to hour and mm to minute
    // normalize time


    void writeTime() const;
    //display the time on screen in HH:MM format



    //ACCESS MEMBER FUNCTIONS
    int getHour()const;
    // return the hour value for the current time

    int getMinute()const;
    //return the minute value for the current time

    private:

    int hour,minute; //data members

    void normalizeTime();
    // Utility function that sets the hours value in the range 0 -23
    // and the minute value in the range 0 - 59

    };

    #endif


    And then my class implementations are in my time24.cpp file
    which is as follows:

    #include <iostream>
    #include "time24.h"


    using namespace std;



    //FUNCTION normalizeTime(): Sets hours and minutes within their proper ranges
    void time24::normalizeTime()
    {
    int extraHours = minute/60;

    //set minutes in range from 0 to 59
    minute %=60;

    //Update hour. Set in range 0-23
    hour = (hour + extraHours) % 24;
    }
    // CONSTRUCTOR: creates time 24 object with initial arguments h and m
    time24::time24(int h , int m):hour(h),minute(m)
    {
    //put hour and minute in the correct range
    normalizeTime();
    }
    //FUNCTION duration(): returns time24 object with difference in time in
    //minutes
    time24 time24::duration(const time24& t)
    {
    //convert currTime and time t to minutes
    int currTime = (hour * 60)+minute;
    int tTime = (t.hour*60)+t.minute;

    // return anonymous object
    return time24(0,tTime-currTime);
    }
    //FUNCTION readTime(): reads in time from keyboard

    void time24::readTime()
    {
    char delimeter;
    cin>>hour>>delimeter>>minute;
    //make sure hour and minute are in range
    normalizeTime();
    }
    //FUNCTION getHour():Accesses private data member hour

    int time24::getHour()const
    {
    return hour;
    }
    //FUNCTION getMinute(): Accesses private data member minute

    int time24::getMinute()const
    {
    return minute;
    }
    //FUNCTION writeTime(): displays time24 object in HH:MM format on screen

    void time24::writeTime()const
    {
    cout<<hour<<":"<<minute<<endl;
    }
    //Function addTime(int m): adds m number of minutes to a time24 object

    void time24::addTime(int m)
    {
    minute += m;
    normalizeTime();
    }

    Ive reviewed my work several times and cant figure out while my files wont link ....
    I recieve the following errors

    Error 1 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup MSVCRTD.lib DYLAN


    Error 2 fatal error LNK1120: 1 unresolved externals C:\Documents and Settings\dh63265\My Documents\Visual Studio 2008\Projects\DYLAN\Debug\DYLAN.exe DYLAN


    Ive tried googling these errors and tried the necessary fixes I could find but messing with linker advanced properties which was usually the recommended fix did not work.

    PLEASE HELP!!! thanks in advance.
    Attached Files Attached Files

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,585

    Re: Linking errors

    The reason for the error is quite simple: That's no complete C++ program because it doesn't contain a main() function.

    As-is, it's solely a class definition, of which you only can produce an .obj file (that is the output of the compiler, no need to link) or at best a .lib file. Either of these could then be linked to a compete program that uses the class.

    So you have to either 1) add a main() function or 2) change your project settings to produce either an .obj or a .lib file. You didn't say in your post which development package you use. Posting in the Visual C++ forum suggests that it is likely to be VC++. But which version? Knowing that, probably someone can tell you which settings to change.

    HTH

    Please use code tags the next time you post code. It will make your code much more readable and help people who are going to help you.

    Ah, and... Welcome to CodeGuru!

  3. #3
    Join Date
    Sep 2010
    Posts
    3

    Re: Linking errors

    Awesome... I understand that sooo I just need to compile the file without linking?? Unless of course I include a source file with an executable main() function. I currently use linux EMACS system for my computer science course and creating .obj files in emacs is very easy. However, outside the course I use microsoft visual studious 2008 and I have NO idea how to create obj files using the program. Could anyone help me solve this problem? Thanks in advance

  4. #4
    Join Date
    Sep 2010
    Posts
    3

    Re: Linking errors

    Awesome... I understand that sooo I just need to compile the file without linking?? Unless of course I include a source file with an executable main() function. I currently use linux EMACS system for my computer science course and creating .obj files in emacs is very easy. However, outside the course I use microsoft visual studious 2008 and I have NO idea how to create obj files using the program. Could anyone help me solve this problem? Thanks Eri523.. Thanks to all in advance.


    p.s. im a noob ill work on the code tagging

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,585

    Re: Linking errors

    Quote Originally Posted by codenooblet View Post
    I understand that sooo I just need to compile the file without linking?? Unless of course I include a source file with an executable main() function.
    Exactly that.

    However, outside the course I use microsoft visual studious 2008 and I have NO idea how to create obj files using the program.
    The MS compiler has an optin /c that means "don't link". I'm not sure how to correctly set that from the IDE, however.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center