简体   繁体   English

DevC ++编译器错误

[英]DevC++ compiler error

I am to write a test program that tests various operations on the class as designed in a previous problem; 我要编写一个测试程序,以测试上一个问题中设计的类上的各种操作; showing the definition of clockType overloading as member functions. clockType重载的定义显示为成员函数。 I get the following error when I compile it using Dev C++ compiler. 使用Dev C ++编译器进行编译时出现以下错误。

The error reads: 该错误显示为:

[link error] undefined reference "WinMain@16'
Id returned 1 exit status

This is my code: 这是我的代码:

#include <iostream>

using namespace std;

class clockType
{
public:
      void setTime (int hours, int minutes, int seconds);
      void getTime (int& hours, int& minutes, int& seconds) const;     
      clockType operator++();
      bool operator==(const clockType& otherClock) const;
      bool operator!= (const clockType& otherClock) const;
      bool operator<=(const clockType& otherClock) const;
      bool operator<(const clockType& otherClock) const;
      bool operator>=(const clockType& otherClock) const;
      bool operator>(const clockType& otherClock) const;
      clockType ();
      clockType (int hours = 0, int minutes = 0, int seconds = 0);
private:
        int hr;
        int min;
        int sec;
};
clockType clockType::operator++()
{
          sec++;
          if (sec > 59)
          {
                  sec = 0;
                  min++;
                  if (min > 59)
                  {
                          min = 0;
                          hr++;
                          if (hr > 23)
                          hr = 0;
                  }
          }
          return *this;
}
bool clockType::operator==(const clockType& otherClock) const
{
     return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec);
}
bool clockType::operator<=(const clockType& otherClock) const
{
     return ((hr < otherClock.hr) || (hr == otherClock.hr && min < otherClock.min) || (hr == otherClock.hr && min == otherClock.min && sec <= otherClock.sec));
}
bool clockType::operator!=(const clockType& otherClock) const
{
          return (hr != otherClock.hr || min != otherClock.min || sec != otherClock.sec);
}
bool clockType::operator<(const clockType& otherClock) const
{
     return ((hr < otherClock.hr) || (hr == otherClock.hr && min < otherClock.min) || (hr == otherClock.hr && min == otherClock.min && sec < otherClock.sec));
}
bool clockType::operator>=(const clockType& otherClock) const
{
     return ((hr > otherClock.hr) || (hr == otherClock.hr && min > otherClock.min) || (hr == otherClock.hr && min == otherClock.min && sec >= otherClock.sec));
}
bool clockType::operator>(const clockType& otherClock) const
{
     return ((hr > otherClock.hr) || (hr == otherClock.hr && min > otherClock.min) || (hr == otherClock.hr && min == otherClock.min && sec > otherClock.sec));
}

void clockType::setTime(int hours, int minutes, int seconds)
{
     if (0 <= hours && hours < 24)
     hr = hours;
     else
     hr = 0;
     if (0 <= minutes && minutes < 60)
     min = minutes;
     else
     min = 0;
     if (0 <= seconds && seconds < 60)
     sec = seconds;
     else
     sec = 0;
}
void clockType::getTime(int& hours, int& minutes, int& seconds)const
{
   hours = hr;
   minutes = min;
   seconds = sec;
}
clockType::clockType(int hours, int minutes, int seconds)
{
 setTime(hours, minutes, seconds);
}

Any help on this matter would be greatly appreciated. 在此问题上的任何帮助将不胜感激。 I am not real good at programming, and I just can't find out why I would be getting this type of error. 我不是很擅长编程,我只是无法找出为什么会出现这种错误。 I haven't on my other programs that I have written for class thus far. 到目前为止,我还没有参加我为课堂编写的其他程序。

You are missing the int main() function. 您缺少int main()函数。 A class doesn't really compile into a program by itself, you could compile it into a DLL , but not into a executable without the main function. 一个类本身并不能真正编译为程序,您可以将其编译为DLL ,但不能编译为没有main函数的可执行文件。 More specifically, the program will compile, but the linker is complaining a it needs to have an entry point for the program. 更具体地说,该程序将编译,但是链接器抱怨它需要该程序的入口点。

If you put this at the bottom of your program: however this merely causes the undefined reference to go away. 如果将其放在程序的底部:但是,这只会导致未定义的引用消失。

int main(){
    return 0;
}

In order to actually test your program, you will need to run some tests, maybe something like this: 为了实际测试您的程序,您将需要运行一些测试,也许是这样的:

#include <iostream>

// your class code here.

int main(){
    Clocktype clock;
    clock.setTime(15, 42, 13);
    int h, m, s;
    clock.getTime(&h, &m, &s);
    std::cout << h << m << s << std::endl;
    return 0;
}

It is not a compiler but a linking error, it says that the linker is trying to find the main function (to provide the entry point for a Windows application). 它不是编译器,而是链接错误,它表示链接器正在尝试查找主要功能(为Windows应用程序提供入口点)。

The compilation itself does work well (the linking step works after the compilation is successful). 编译本身运行良好(编译成功后,链接步骤将起作用)。

There absolutely is an IDE called Dev-C++ made by the company Bloodshed, I believe it is discontinued but still fairly compatible and favoured as it is free and easy to use with a nice interface. 绝对有Bloodshed公司生产的称为Dev-C ++的IDE,我相信它已经停产了,但仍然相当兼容并且受到青睐,因为它是免费的且易于使用且具有良好的界面。

As to the OP's question, you need to be sure you have an application entry point, in your project settings (I haven't used Dev-C++ in years so take a look around), you will find something saying application type, the options listed will be something like 关于OP的问题,您需要确保在项目设置中有一个应用程序入口点(我多年来没有使用过Dev-C ++,所以请环顾四周),您会发现一些内容,说明应用程序类型,选项列出将是这样的

Console Application
GUI Application or maybe Win32 Application
Static Library
Dynamic Library

These are all different types of applications and it looks like by default your program has the Win32 Application setting, in this case the linker is looking for WinMain however you have not provided one in your project, if you wish for a simple console application change the application type to that setting, which will require a simple main function, examples of the application entry point functions for both Win32 and Console are 这些都是不同类型的应用程序,默认情况下,您的程序具有Win32 Application设置,在这种情况下,链接器正在寻找WinMain,但是您没有在项目中提供一个,如果您希望使用简单的控制台应用程序,请更改该设置的应用程序类型,这将需要一个简单的主要功能,Win32和Console的应用程序入口点功能示例如下:

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iShow)
{
return 0;
}

Or for a simple Console Application 或用于简单的控制台应用程序

int main(int argc, char* argv[])
{
return 0;
}

These functions are where you need to plug in the rest of your application in order for it to do anything, without this entry point your application cannot even be properly compiled and linked. 这些功能是您需要插入应用程序其余部分以便执行任何操作的地方,没有此入口点,您的应用程序甚至无法正确编译和链接。

An example of making your application actually use your class (without using any functions) 使您的应用程序实际使用您的类的示例(不使用任何函数)

int main(int argc, char* argv[])
{
   clockType  myClockType;
   myClockType.setTime(12, 30, 45);

   return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM