在课本例题中发现的一个问题,关于main函数类型
c++中一般函数的类型定义为int,可这段程序怎么加int以后在VC++中反出错了?
错误信息如下:
--------------------Configuration: 0608(2) - Win32 Debug--------------------
Compiling...
new.cpp
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/0608(2).exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.
0608(2).exe - 1 error(s), 0 warning(s)
程序如下:
#include <iostream.h>
#include <stdlib.h>
class timer{
public:
timer()
{seconds=0;}
timer(char* t)
{seconds=atoi(t);}
timer(int t)
{seconds=t;}
timer(int min,int sec)
{seconds=min*60+sec;}
int gettime()
{return seconds;}
private:
int seconds;
}
main()//为什么在“main”前面加“int”编译会出错?
{
timer a,b(10),c( "20 "),d(1,10);
cout < < "seconds1= " < <a.gettime() < <endl;
cout < < "seconds2= " < <b.gettime() < <endl;
cout < < "seconds3= " < <c.gettime() < <endl;
cout < < "seconds4= " < <d.gettime() < <endl;
return 0;
}
[解决办法]
你的错误在于类 timer 的定义后面没有加 ;
和 main 返回什么没关系。
没加 ; 的话编译器可能认为 main 返回类型是 timer.
[解决办法]
private:
int seconds;
}; // <====要加分号
[解决办法]
class T{};
==========
加上分号编译器才知道这个类完整定义啦