以前一直使用WINDOWS下的编译工具VC和BORNLAND C++ BUILDER写些小程序,没在LINUX下做过这样的工作。今天写了一段简单的"HELLO WORLD"程序,在VC.NET下编译没问题,但是在Linux的GCC下编译一直不能通过,一直找不到原因,相当郁闷。哪位看官知道,麻烦指点一下。
#include <iostream>
#include <stdlib.h>
using namespace std;
class Sample
{
public:
int x,y;
Sample(){x=0;y=0;cout<<"Create Class Sample!"<<endl;}
Sample(int a,int b)
{
x=a;y=b;
cout<<"Create Class Sample!"<<endl;
}
~Sample()
{
if(x==y)
cout << "x=y" <<endl;
else
cout<<"x!=y"<<endl;
cout<<"Delete Class Sample!/n"<<endl;
}
void disp()
{
cout<<"x="<<x<<",y="<<y<<endl;
}
};
void main()
{
int x=2;
int y=3;
Sample s1(x,y);
s1.disp();
}
忙活了半天,找到问题了,首先将程序改成这样:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Sample
{
public:
int x,y;
Sample(){x=0;y=0;cout<<"Create Class Sample!"<<endl;}
Sample(int a,int b)
{
x=a;y=b;
cout<<"Create Class Sample!"<<endl;
}
~Sample()
{
if(x==y)
cout << "x=y" <<endl;
else
cout<<"x!=y"<<endl;
cout<<"Delete Class Sample!/n"<<endl;
}
void disp()
{
cout<<"x="<<x<<",y="<<y<<endl;
}
};
int main(int argc,char *argv[])
{
int x=2;
int y=3;
Sample s1(x,y);
s1.disp();
return 0;
}
这就符合标准C++了。
另外,我刚才在编译是用的命令是gcc -o proj001 proj001.cpp
这个命令是用来编译C语言的,编译C++应该换成g++ -o proj001 proj001.cpp