DevC++ 编译器错误

DevC++ compiler error

本文关键字:错误 编译器 DevC++      更新时间:2023-10-16

我将编写一个测试程序,按照上一个问题中的设计测试类上的各种操作;将clockType重载的定义显示为成员函数。当我使用 Dev C++ 编译器编译它时,我收到以下错误。

错误如下:

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

这是我的代码:

#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);
}

对此事的任何帮助将不胜感激。我并不擅长编程,我只是不知道为什么会出现这种类型的错误。到目前为止,我还没有为课堂编写的其他程序。

Bloodshed公司绝对有一个名为Dev-C++的IDE,我相信它已经停产,但仍然相当兼容和青睐,因为它是免费的,易于使用,界面很好。

至于OP的问题,你需要确保你有一个应用程序入口点,在你的项目设置中(我已经很多年没有使用Dev-C++了,所以看看周围),你会发现一些东西说应用程序类型,列出的选项将是这样的

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

这些都是不同类型的应用程序,看起来默认情况下您的程序具有 Win32 应用程序设置,在这种情况下,链接器正在寻找 WinMain,但是您没有在项目中提供一个,如果您希望一个简单的控制台应用程序,请将应用程序类型更改为该设置,这将需要一个简单的 main 函数, Win32 和控制台的应用程序入口点函数的示例包括

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

或用于简单的控制台应用程序

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

在这些函数中,您需要插入应用程序的其余部分才能使其执行任何操作,如果没有此入口点,您的应用程序甚至无法正确编译和链接。

使应用程序实际使用类的示例(不使用任何函数)

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

您缺少int main()函数。一个类本身并没有真正编译成一个程序,你可以把它编译成一个DLL,但不能编译成没有main函数的可执行文件。更具体地说,程序将编译,但链接器抱怨它需要程序的入口点。

如果你把它放在程序的底部:然而,这只会导致未定义的引用消失。

int main(){
    return 0;
}

为了实际测试你的程序,你需要运行一些测试,也许是这样的:

#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;
}

它不是一个编译器,而是一个链接错误,它说链接器正在尝试查找主函数(为 Windows 应用程序提供入口点)。

编译本身确实运行良好(编译成功后链接步骤有效)。