看下哪错了!!!

ws274888829 2008-12-31 02:31:12
1、#include <iostream.h>
class com
{ int x,y,c;
public:
com(int i=0,int j=0);
void f() const;
}
void com::f() const
{cout<<x<<” “<<y<<endl;
c++;
}
2、#include <iostream.h>
class sp
{
public:
sp(int a=0,b=1);
disp();
private:
int x,y;
};
sp::sp(int a=0,int b=1)
{
x=a; y=b;
}
void sp:disp()
{
cout<<”x=”<<x<<”,y=”<<y<<endl;
}
3、#include <iostream.h>
       class point
{
int x1,x2;
public:
point(int x,int y);
……………..
};
void main()
{
point data(5,5);
cout<<data.x1<<endl;
cout<<data.x2<<endl;
}
...全文
162 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
czbever 2009-01-04
  • 打赏
  • 举报
回复
class com
{ int x,y,c;
public:
com(int i=0,int j=0);
void f() const;
}

这里少了个逗号
hoomien 2009-01-04
  • 打赏
  • 举报
回复
jf
herman~~ 2009-01-04
  • 打赏
  • 举报
回复
mark
feng_xw 2009-01-03
  • 打赏
  • 举报
回复
1、#include <iostream.h>
class com
{ int x,y,c;
public:
com(int i=0,int j=0);
void f() const;
}
void com::f() const
{cout < <x < <” “ < <y < <endl;
c++;
} // 少分号,且const 成员函数不能修改类的成员变量。
2、#include <iostream.h>
class sp
{
public:
sp(int a=0,b=1);
disp();
private:
int x,y;
};
sp::sp(int a=0,int b=1)
{
x=a; y=b;
}
void sp:disp()
{
cout < <”x=” < <x < <”,y=” < <y < <endl;
} // 构造析构函数无返回值,其它函数无返回值时应明确指明返回void
3、#include <iostream.h>
       class point
{
int x1,x2;
public:
point(int x,int y);
……………..
};
void main()
{
point data(5,5);
cout < <data.x1 < <endl;
cout < <data.x2 < <endl;
} // x1 x2 为私有变量不能直接访问,可添加get set 等访问控制函数
genius_tong 2009-01-02
  • 打赏
  • 举报
回复
日,来晚了……
xuruichen 2009-01-02
  • 打赏
  • 举报
回复
日,都被楼上说完了,


类定义体后面的分号很容易忘记。


class 的默认成员是private的
struct 的默认成员是public的
他们的默认继承类型也是这样的。


成员函数设成const的,那么this指针指向的对象就是const的,就是该成员函数不能改变调用它的类对象。


重复一下。哈哈哈,小问题,但是容易出错。

l176266956 2009-01-02
  • 打赏
  • 举报
回复
构造函数少了花括号。
HEROPJP 2009-01-02
  • 打赏
  • 举报
回复
路过。。。。。。。。。。。。。
星羽 2009-01-02
  • 打赏
  • 举报
回复
jf
wwf06802136 2009-01-02
  • 打赏
  • 举报
回复
学习
guanyin_2008 2009-01-02
  • 打赏
  • 举报
回复
楼上说的很清楚了。
elegant87 2009-01-02
  • 打赏
  • 举报
回复
JF!
jiangheng2008 2008-12-31
  • 打赏
  • 举报
回复
3.class成员默认为private
cout < <data.x1 < <endl;
cout < <data.x2 < <endl; //不能访问类的私有成员
xiaoyisnail 2008-12-31
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 jiangheng2008 的回复:]
1.C/C++ codevoidcom::f()const{cout<<x<<” “<<y<<endl;
c++;
}
不能要const。
[/Quote]

呀,大意了,不应该

#include <iostream.h>

class com
{
int x,y,c;
public:
com(int i=0,int j=0);
void f();
};

void com::f()
{
cout <<x <<" "<<y <<endl;
c++;
}
jiangheng2008 2008-12-31
  • 打赏
  • 举报
回复
1.

void com::f() const
{cout < <x < <” “ < <y < <endl;
c++;
}

不能要const。
xiaoyisnail 2008-12-31
  • 打赏
  • 举报
回复
1.漏了分号结束类定义

#include <iostream.h>
class com
{
int x,y,c;
public:
com(int i=0,int j=0);
void f() const;
};

void com::f() const
{
cout <<x <<" "<<y <<endl;
c++;
}


2.看代码吧

#include <iostream.h>
class sp
{
public:
sp(int a=0, int b=1);
void disp();
private:
int x,y;
};

sp::sp(int a,int b)
{
x=a;
y=b;
}

void sp::disp()
{
cout<<"x= "<<x<<",y="<<y<<endl;
}


3.看代码

#include <iostream.h>
class point
{
public:
int x1,x2;
public:
point(int x,int y):x1(x),x2(y){}
};

void main()
{
point data(5,5);
cout <<data.x1 <<endl;
cout <<data.x2 <<endl;
}

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧