我要编写一个测试程序,按照上一个问题中的设计测试类上的各种操作;将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);
}在这件事上的任何帮助都将非常感谢。我并不擅长编程,我就是找不出为什么我会遇到这种类型的错误。到目前为止,我还没有在我为类编写的其他程序中。
发布于 2012-08-12 07:26:06
您缺少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;
}发布于 2012-08-12 07:45:08
绝对有一个叫Dev-C++的IDE是由血腥公司开发的,我相信它已经停产了,但仍然相当兼容和受欢迎,因为它是免费的,使用起来很容易,界面也很好。
至于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;
}发布于 2012-08-12 07:27:54
这不是一个编译器,而是一个链接错误,它说明链接器正在尝试查找main函数(为Windows应用程序提供入口点)。
编译本身确实工作得很好(链接步骤在编译成功后工作)。
https://stackoverflow.com/questions/11918470
复制相似问题