Undefined reference to WinMain@16 - Codeblocks

孤街浪徒 提交于 2019-12-13 07:42:55

问题


I have read: undefined reference to `WinMain@16' & still don't understand my problem.

I had a program that was working. Added a class but had not implemented it into the program yet just wrote the header and .cpp file. The program previous to just adding this class worked and now it doesn't.

Error states... **file address....libmingw.32.a(main.o):main.c:(.text.startup+0xa7)

Header file

        #ifndef DATE_H
        #define DATE_H

        #include <iostream>
        #include <string>

        class Date
        {
            public:
                Date();

                //Setters
                void SetDay(unsigned dy);

                void SetMonth(std::string month);

                void SetYear(unsigned mnth);

                //Getters
                unsigned GetDay() const;

                std::string GetMonth() const;

                unsigned GetYear() const;

            private:
                unsigned day, year;
                std::string month;
        };

        #endif // DATE_H

.cpp file

        #include "Date.h"

        Date::Date()
        {
            day = 0;
            year = 0;
            month = "Not Set";
        }

        //Setters
        void Date::SetDay(unsigned dy)
        {
            day = dy;
        }

        void Date::SetMonth(std::string mnth)
        {
            month = mnth;
        }

        void Date::SetYear(unsigned yr)
        {
            year = yr;
        }

        //Getters
        unsigned Date::GetDay() const
        {
            return day;
        }

        unsigned Date::GetYear() const
        {
            return year;
        }

        std::string Date::GetMonth() const
        {
            return month;
        }

my main which I call it in, just because I wasn't sure if the error was because it wasn't being called or something like that is:

        #include <iostream>
        #include <fstream>
        #include "unit.h"  // Unit class declaration
        #include "regist.h"  // Registration class declaration
        #include "date.h"

        using namespace std;

        // Main program:
        // Open an input file stream, read a Registration object,
        // including its list of courses. Redisplay all information,
        // and calculate the total credits taken. Write the results
        // to a file stream.


        int main()
        {
            ifstream infile( "testin.txt" );
            if( !infile ) return -1;

            Registration R;
            Date D;
            infile >> R;

            ofstream ofile( "testout.txt" );

            ofile   << R;

            cout << "\nComputation completed. Check output file. \n";
            cout << "\n Day is " << D.GetDay;

            return 0;
        }

Day does not get set in the overloaded >> operator relating to Registration. It is set by the basic constructor.

Again I haven't added this class in to the program as it is going to be I'm just trying to compile after it was write and added in a basic testing manner. Testing through main.

Thanks in advance. =D


回答1:


The problem is that your new project has been created as a Win32 GUI project, when it should have been created as a Console application. The former requires that a function with the signature int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) exists, while the latter requires one of the usual form taken for C or C++ projects, namely int main() or int main(char *argv[], int argc).

Either create a new project of the console type and copy your code into it, or use the 'sticky-tape' solution, and change int main() to int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) You would also need to navigate to the project's properties and change the Build Target from GUI application to Console application - failing to do so would mean that you would not see any output, since you're using cout, which prints to stdout, which is shown by the console. This window is always available for a console application, but is only available for the Debug version of a GUI application.

I reccomend doing it properly and creating a new project of the appropriate type, namely a Console application. :)




回答2:


Change the type of your application under your project settings to Console. WinMain16 is related to Windows graphical applications. I believe you do need to set a special preprocessor flag or include a library of some sort to get it to work properly if you keep it as a Windows graphical application, but in this case, the easiest fix would be to just get a console application.

Also, maybe adding the -mwindows flag can help you.



来源:https://stackoverflow.com/questions/29404780/undefined-reference-to-winmain16-codeblocks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!