Thread: Trying to use class files, getting "undefined reference to `WinMain@16'" error

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    Trying to use class files, getting "undefined reference to `WinMain@16'" error

    I am building an application that calculates travel time necessary to reach a user selected destination from predetermined city based upon an estimated travel speed determined by the user. For this program I am to utilize classes/class source files. When compiling my source file I continuously get the error “undefined reference to `WinMain@16’” and due to my being unfamiliar with the usage of source files I am not sure how to fix this issue. Any help would be appreciated with regard to pointing me in the right direction as to properly utilizing source files. Thanks in advance for the help.

    Header file:

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    class Trip
    {
        private:
            string destination;
            double distance;
    
    
        public:
            void TripValue(string b, double c);
            void TripTime(Trip *a);
    };
    Source file:

    Code:
    #include "Trip.h"
    #include <iostream>
    
    
    using namespace std;
    
    
    void Trip::TripValue(string b, double c)
    {
        destination = b;
        distance = c;
    }
    
    
    void Trip::TripTime(Trip *a)
    {
        double user_speed;
        double time;
    
    
        cout << "Please enter your estimated travel speed in miles per hour: ";
        cin >> user_speed;
        cout << endl;
    
    
        time = (a->distance / user_speed);
    
    
        cout << endl;
        cout << "Your estimated travel time to " << a->destination << " is "
            << time << " hours.\n";
        cout << endl;
    }
    Application code:

    Code:
    #include <iostream>
    #include "Trip.h"
    
    
    using namespace std;
    
    
    int main()
    {
        Trip StL, Indy, Det, Nash, Dal, Den, NY, LA, Mia, Sea;
    
    
        int choice;
    
    
        StL.TripValue("St. Louis", 297.34);
        Indy.TripValue("Indianapolis", 184.78);
        Det.TripValue("Detroit", 282.73);
        Nash.TripValue("Nashville", 441.02);
        Dal.TripValue("Dallas", 925.91);
        Den.TripValue("Denver", 1004.93);
        NY.TripValue("New York", 791.50);
        LA.TripValue("Los Angeles", 2017.74);
        Mia.TripValue("Miami", 1363.55);
        Sea.TripValue("Seattle", 2032.31);
    
    
        do
        {
            cout << "=================================Trip Calulator=================================\n";
            cout << "Select your destination from Chicago:\n";
            cout << endl;
            cout << " 1. St. Louis\n";
            cout << " 2. Indianapolis\n";
            cout << " 3. Detroit\n";
            cout << " 4. Nashville\n";
            cout << " 5. Dallas\n";
            cout << " 6. Denver\n";
            cout << " 7. New York\n";
            cout << " 8. Los Angeles\n";
            cout << " 9. Miami\n";
            cout << "10. Seattle\n";
            cout << endl;
            cout << " 0. Exit\n";
            cout << "================================================================================\n";
    
    
            cin >> choice;
    
    
            cout << endl;
    
    
            switch (choice)
            {
                case 1:
                    StL.TripTime(&StL);
                    break;
                case 2:
                    Indy.TripTime(&Indy);
                    break;
                case 3:
                    Det.TripTime(&Det);
                    break;
                case 4:
                    Nash.TripTime(&Nash);
                    break;
                case 5:
                    Dal.TripTime(&Dal);
                    break;
                case 6:
                    Den.TripTime(&Den);
                    break;
                case 7:
                    NY.TripTime(&NY);
                    break;
                case 8:
                    LA.TripTime(&LA);
                    break;
                case 9:
                    Mia.TripTime(&Mia);
                    break;
                case 10:
                    Sea.TripTime(&Sea);
                    break;
            }
        } while (choice != 0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you are using an IDE that has a project concept, make sure that all the files are in the same project. Otherwise, make sure that you compile and link altogether; if you want to compile each source file separately, make sure you don't link (to build the executable program).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    I'm using Code::Blocks and to this point have only utilized the feature to make a new "empty file" and have never explored the project option listed. Would re-creating this specifically as a project and then re-coding properly solve this issue I'm having?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, I think it would.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > When compiling my source file I continuously get the error “undefined reference to `WinMain@16’”
    It would seem that you chose "win32 GUI program" as the project type.

    You need to make sure you choose a console project.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't Compile a code!!! I ran into "undefined reference to" error.
    By andereasmehdi in forum Linux Programming
    Replies: 19
    Last Post: 06-28-2011, 05:45 AM
  2. Replies: 4
    Last Post: 12-16-2008, 08:00 PM
  3. Error "undefined reference to"
    By Bargi in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2007, 05:12 AM
  4. Error "undefined reference to"
    By Bargi in forum Linux Programming
    Replies: 1
    Last Post: 09-13-2007, 04:47 AM
  5. "undefined reference" in my Class
    By bumcheekcity in forum C++ Programming
    Replies: 8
    Last Post: 04-08-2007, 01:40 PM