注册 登录
编程论坛 C++教室

请大神指点!输出有误........

IT男year 发布于 2013-10-16 16:51, 628 次点击
#include<iostream>
using namespace std;
class s
{
private:
    int x,y,z;
public:
    s(int x,int y,int z);
    int getx();
};
s::s(int x,int y,int z)
{
    x=x;
    y=y;
    z=z;
}
int s::getx()
{   
    return x;
}
int main()
{
    s p1(2,0,0);
    cout<<p1.getx();
    return 0;
}
为什么输不出结果呀?哪里错了?请大神详解呀??????
5 回复
#2
blueskiner2013-10-16 16:55
s::s(int x,int y,int z)
{
    this->x=x;
    this->y=y;
    this->z=z;
}

代码风格很重要。。。变量命名要遵循一些格式。
#3
IT男year2013-10-16 17:24
回复 2楼 blueskiner
大神能否解释的更加详细些?
#4
blueskiner2013-10-16 17:28
构造函数的参数跟成员变量的命名相同,编译器不知道该解释那个。添加this指针可以绑定变量的依赖
#5
序曲萧邦2013-10-18 17:31
this指针。在后面有学到。学到了就知道
#6
dengdaidw2013-10-20 01:08
你把构造函数里的参数名改成别的就可以了,因为变量同名时,存在作用域的重叠,私有成员变量被屏蔽了,相当于1=1,
1