DevC++编译器错误
我要编写一个测试程序,测试上一个问题中设计的类上的各种操作;显示clockType
重载作为成员函数的定义。当我使用Dev C++编译器进行编译时,出现以下错误。DevC++编译器错误
错误读取:
[link error] undefined reference "[email protected]'
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);
}
在这个问题上的任何帮助,将不胜感激。我在编程方面并不擅长,但我只是无法弄清楚为什么我会遇到这种类型的错误。迄今为止,我还没有上过我写过的其他课程。
您缺少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带来更多问题,因为他的程序仍然不会执行任何操作。 – 2012-08-11 23:32:51
这是真的,让我更新它。 – Annabelle 2012-08-11 23:42:09
好吧,现在我明白我错过了什么,我实际上错过了整个可执行程序!在哪里实际上将分配的变量并通过测试运行它们!谢谢,我知道我不应该得到这样的事情,但是我没有足够的理解它的原因。谢谢。 – user1592770 2012-08-12 00:10:34
它不是一个编译器,但一个链接错误,它说,连接器正试图找到主要功能(为Windows应用程序提供入口点)。
编译本身确实工作正常(编译成功后链接步骤工作)。
好吧,我得到你说的这是一个链接错误,当我在查看“int main() {return 0;}部分,但是,这是我书中的一个例子,它不使用书中的int main ...在上面的例子中,我会应用那个部分,我在不同的地方尝试过它并得到 – user1592770 2012-08-12 00:05:42
您编写一个类并编译它,它会被转换为机器代码,该机器代码可以作为库的一部分(静态.lib或动态.dll/.so)或作为一个程序结束。一个程序的情况下,它需要由main定义的入口点。要从机器代码中获得一个库或程序,必须将所需的参数传递给链接器(我不记得哪个是要指定的,它有已经很长时间了,因为我已经使用C++,而且大多数IDE都会简化它们) – SJuan76 2012-08-12 00:42:06
绝对有一个名为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;
}
Dev-C + +是青睐的对立面。这是一个可怕的憎恶,任何人都不应该被卡住。 – Puppy 2012-08-12 14:47:32
这是一个意见,Dev-C++可能已经过时,但它是一个非常完善的IDE,带有一个很棒的插件包系统,很好的自动完成功能,并且在一点上具有良好的兼容性。只是说这是一个没有理由的憎恶不是一个好的答案。 – user1294021 2012-08-16 02:41:40
他们在Windows上创建了一个可替代的IDE,做得很好,我很想看看你已经构建的IDE以及它的优点。 Dev-C++也是在Windows上最容易与GTK一起使用的,但它永远不会被构建,我想它会完全超出可用范围。 – user1294021 2012-08-16 02:43:49
你如何建立你的程序的一个例子吗?这听起来像你缺少一个'main()'函数。 – 2012-08-11 23:26:38
没有像DevC++这样的C++编译器。 – 2012-08-11 23:32:39
你的意思是Visual Studio而不是DevC++? – joce 2012-08-11 23:36:50