以前一直使用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