【问题标题】:DevC++ compiler error开发 C++ 编译器错误
【发布时间】:2012-08-11 23:24:45
【问题描述】:

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

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

【问题讨论】:

  • 你是如何构建你的程序的?听起来您缺少 main() 函数。
  • 没有像 DevC++ 这样的 C++ 编译器。
  • 你的意思是 Visual Studio 而不是 DevC++?
  • @Joce:他的意思是 gcc。但可悲的是,他并不真正知道......
  • 如果您使用的是原始 Bloodshed 版本,请避免使用 Dev-C++。它至少已经过时并且存在很大问题。相关的tag wiki 确实提供了更多信息。

标签: c++ dev-c++


【解决方案1】:

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

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

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

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

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

【讨论】:

  • Dev-C++ 是 favoured 的反面。这是一个可怕的可憎之物,任何人都不应该被困在其中。
  • 这是一种观点,Dev-C++ 可能已经过时,但它是一个构建良好的 IDE,具有出色的插件包系统、良好的自动完成功能,并且在某一点上具有良好的兼容性。无缘无故地说它是可憎的不是一个好的答案。
  • 他们在创建可在 Windows 上使用的替代 IDE 方面做得很好,我很想看看您构建的 IDE 以及它有多好。 Dev-C++ 也是最容易在 Windows 上与 GTK 一起使用的,但是因为它再也不会被构建,我想它会完全超出可用性范围。
【解决方案2】:

您缺少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;
}

【讨论】:

  • 您建议的解决方案是正确的,因为它会使错误消失,但它只会给 OP 带来更多问题,因为他的程序仍然不会任何事情.
  • 好的,现在我明白我缺少什么了,我实际上缺少了整个可执行程序!它实际上会在哪里获取分配的变量并通过测试运行它们!谢谢,我知道我不应该得到这样的东西,但我并没有真正理解它的原因。谢谢。
  • 没问题,我也遇到过。
  • 呃,&amp;h, &amp;m, &amp;s?供参考?
  • 它是必需的,因为函数需要一个引用。 void clockType::getTime(int&amp; hours, int&amp; minutes, int&amp; seconds)
【解决方案3】:

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

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

【讨论】:

  • 好的,我明白你所说的它是一个链接错误,当我在搜索答案之前我没有看到“int main () { return 0; } 部分,但是,这个是我书中的一个例子,它没有使用书中的 int main ...在像上面这样的情况下,我将在哪里应用该部分,我在不同的地方尝试了它并得到了不同的错误。
  • 你写一个类并编译它;它被翻译成机器代码。该机器代码可以作为库的一部分(静态 .lib 或动态 .dll/.so)或作为程序结束。在程序的情况下,它需要由 main 定义的入口点。要从机器代码中获取库或程序,您必须将所需的参数传递给链接器(我不记得要指定哪些参数,我已经很长时间没有使用 C++ 并且大多数 IDE 确实简化了它们)
猜你喜欢
  • 2015-07-09
  • 2015-04-15
  • 2010-11-24
  • 2016-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多