DevC++ 编译器错误
DevC++ compiler error
我将编写一个测试程序,按照上一个问题中的设计测试类上的各种操作;将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 应用程序提供入口点)。
编译本身确实运行良好(编译成功后链接步骤有效)。
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++,我收到一个无法理解的编译器错误
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 我收到同义重复编译器错误。我应该如何修复"类型"X"的参数与类型"X"的参数不兼容?
- 重载方法的方式会在使用临时调用时生成编译器错误
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- 有没有办法让编译器在我放置字符串而不是 nlohmann::json 对象时抛出错误?
- 致命错误 C1001 :vs2017 15.8.4 的内部错误(编译器文件"msc1.cpp",第
- C++2440错误-编译器认为字符串是常量字符
- Clang 错误 – 编译器错误或缺少一些细节?
- 视觉C++ 2017 错误?编译器优化表达式
- 配置:错误:C++编译器无法创建可执行文件
- PHP PDO_ODBC配置:错误:C 编译器无法创建可执行文件
- 致命错误 C1001:内部编译器错误(编译器文件"MSC1.cpp",第 1794 行)
- 错误:编译器限制达到简单hello world程序的堆限制
- c++链接错误:编译器找不到函数的定义
- 生成 CImg 时出现致命错误(编译器限制)
- 抛出对齐类型时出现 Clang 运行时错误.编译器错误