标准C++?(转自我的其它BLOG)

 

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值