Dev C ++编译器错误

时间:2012-08-11 23:24:46

标签: c++ dev-c++

我要编写一个测试程序,测试上一个问题中设计的类上的各种操作;显示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);
}

对此事的任何帮助将不胜感激。我不擅长编程,我只是无法找出为什么我会遇到这种类型的错误。到目前为止,我没有参加我上课的其他课程。

3 个答案:

答案 0 :(得分: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;
}

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

绝对有一个名为Dev-C ++的IDE由Bloodshed公司制作,我相信它已经停产,但仍然相当兼容和受欢迎,因为它是免费且易于使用的一个漂亮的界面。

关于OP的问题,您需要确保在项目设置中有一个应用程序入口点(我多年没有使用过Dev-C ++,所以看看),你会发现一些说应用程序的类型,列出的选项将类似于

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

这些都是不同类型的应用程序,默认情况下你的程序看起来像Win32应用程序设置,在这种情况下,链接器正在寻找WinMain,但是如果你想要一个简单的控制台,你没有在项目中提供它应用程序将应用程序类型更改为该设置,这将需要一个简单的主函数,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;
}
相关问题