类的成员函数也不能访问私有成员?

lizzoe 2008-10-22 05:21:14
代码如下:

#include <iostream>
using namespace std;

class Item
{
private:
char sex;
int height;
int weight;
public:
Item();
Item& setHeight( int iHeight);
Item& setWeight( int iWeight);
};

Item::Item():sex('m'),height(180),weight(100){};
Item& Item::setHeight( int iHeight)
{
this->height = iHeight;
return *this;
}
Item& Item::setWeight( int iWeight)
{
this->weight = iWeight;
return *this;
}

ostream& operator<< (ostream& c, Item& rhs)
{
c << rhs.sex << "\t" << rhs.height << "\t" << rhs.weight << endl;
return c;
}

int main()
{
Item i;
cout << i << i;
i.setHeight(300);
i.setWeight(500);
Item j = *new Item();
cout << i << j;
}

编译器错误信息:
047.cpp(53) : error C2248: “Item::sex” : 无法访问 private 成员(在“Item”类中声明)
047.cpp(30) : 参见“Item::sex”的声明
047.cpp(28) : 参见“Item”的声明
047.cpp(53) : error C2248: “Item::height” : 无法访问 private 成员(在“Item”类中声明)
047.cpp(31) : 参见“Item::height”的声明
047.cpp(28) : 参见“Item”的声明
047.cpp(53) : error C2248: “Item::weight” : 无法访问 private 成员(在“Item”类中声明)
047.cpp(32) : 参见“Item::weight”的声明
047.cpp(28) : 参见“Item”的声明

...全文
447 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
太乙 2008-10-23
  • 打赏
  • 举报
回复
#include <iostream> 
using namespace std;

class Item
{
private:
char sex;
int height;
int weight;
public:
Item();
Item& setHeight( int iHeight);
Item& setWeight( int iWeight);
friend ostream& operator << (ostream& c, Item& rhs)
{
c << rhs.sex << "\t" << rhs.height << "\t" << rhs.weight << endl;
return c;
}
};

Item::Item():sex('m'),height(180),weight(100){};
Item& Item::setHeight( int iHeight)
{
this->height = iHeight;
return *this;
}
Item& Item::setWeight( int iWeight)
{
this->weight = iWeight;
return *this;
}



int main()
{
Item i;
cout << i << i;
i.setHeight(300);
i.setWeight(500);
Item j = *new Item();
cout << i << j;
}
jackzhhuang 2008-10-23
  • 打赏
  • 举报
回复
5楼正解

1,多了个分号

2,operator是个全局函数(的确应该把operator定义为全局函数,还有一种做法是把它和item类放在同一个名字空间里面。这样做有更好的封装性。)
l176266956 2008-10-23
  • 打赏
  • 举报
回复

[color=#FF0000]1.Item::Item():sex('m'),height(180),weight(100){};最后面多了一个分号
2.在类外面调用了类中的私有成员,但此函数既不是成员函数也不是友元函数。
[/color]

#include <iostream>
using namespace std;

class Item
{
private:
char sex;
int height;
int weight;
public:
Item();
Item& setHeight( int iHeight);
Item& setWeight( int iWeight);
friend ostream& operator<<(ostream& c, Item& rhs)
{
c << rhs.sex << "\t" << rhs.height << "\t" << rhs.weight << endl;
return c;
}
};

Item::Item():sex('m'),height(180),weight(100){};
Item& Item::setHeight( int iHeight)
{
this->height = iHeight;
return *this;
}
Item& Item::setWeight( int iWeight)
{
this->weight = iWeight;
return *this;
}


int main()
{
Item i;
cout << i << i;
i.setHeight(300);
i.setWeight(500);
Item j = *new Item();
cout << i << j;
return 0;
}
ShardowM 2008-10-23
  • 打赏
  • 举报
回复
把ostream& operator < < (ostream& c, Item& rhs)
作为类的友员,1、8楼才是正解

elegant87 2008-10-23
  • 打赏
  • 举报
回复


/*因为这个函数:
ostream& operator < < (ostream& c, Item& rhs)
{
c < < rhs.sex < < "\t" < < rhs.height < < "\t" < < rhs.weight < < endl;
return c;
}
在类外面调用了类中的私有成员,但此函数既不是成员函数也不是友元函数。*/

#include <iostream>

using namespace std;

class Item
{
private:
char sex;
int height;
int weight;
public:
Item();
Item& setHeight( int iHeight);
Item& setWeight( int iWeight);
friend ostream& operator << (ostream& c, Item& rhs);
};

Item::Item():sex('m'),height(180),weight(100){}

Item& Item::setHeight( int iHeight)
{
this->height = iHeight;
return *this;
}
Item& Item::setWeight( int iWeight)
{
this->weight = iWeight;
return *this;
}

ostream& operator << (ostream& c, Item& rhs)
{
c << rhs.sex << "\t" << rhs.height << "\t" << rhs.weight << endl;
return c;
}

int main()
{
Item i;
cout << i << i;
i.setHeight(300);
i.setWeight(500);
Item j = *new Item();
cout << i << j;
system("pause");
return 0;
}
jackzhhuang 2008-10-23
  • 打赏
  • 举报
回复
其实我知道可以用友元

但实际上友元严重破坏封装性,尽量别用友元(虽然effective C++建议这么做)

个人编程观点,拍砖无视。
l176266956 2008-10-23
  • 打赏
  • 举报
回复
补充一下,VC6的编译器不支持类外实现类元函数。所以象下面的代码在VC6中编译不过的,一定要类内实现!
#include <iostream>
using namespace std;

class Item
{
private:
char sex;
int height;
int weight;
public:
Item();
Item& setHeight( int iHeight);
Item& setWeight( int iWeight);
friend ostream& operator<<(ostream& c, Item& rhs);

};

Item::Item():sex('m'),height(180),weight(100){};
Item& Item::setHeight( int iHeight)
{
this->height = iHeight;
return *this;
}
Item& Item::setWeight( int iWeight)
{
this->weight = iWeight;
return *this;
}
ostream& operator<<(ostream& c, Item& rhs)
{
c << rhs.sex << "\t" << rhs.height << "\t" << rhs.weight << endl;
return c;
}


int main()
{
Item i;
cout << i << i;
i.setHeight(300);
i.setWeight(500);
Item j = *new Item();
cout << i << j;
return 0;
}
l176266956 2008-10-23
  • 打赏
  • 举报
回复
补充一下,VC6的编译器不支持类外实现类元函数。所以象下面的代码在VC6中编译不过的,一定要类内实现!
#include <iostream>
using namespace std;

class Item
{
private:
char sex;
int height;
int weight;
public:
Item();
Item& setHeight( int iHeight);
Item& setWeight( int iWeight);
friend ostream& operator<<(ostream& c, Item& rhs);

};

Item::Item():sex('m'),height(180),weight(100){};
Item& Item::setHeight( int iHeight)
{
this->height = iHeight;
return *this;
}
Item& Item::setWeight( int iWeight)
{
this->weight = iWeight;
return *this;
}
ostream& operator<<(ostream& c, Item& rhs)
{
c << rhs.sex << "\t" << rhs.height << "\t" << rhs.weight << endl;
return c;
}


int main()
{
Item i;
cout << i << i;
i.setHeight(300);
i.setWeight(500);
Item j = *new Item();
cout << i << j;
return 0;
}
l176266956 2008-10-23
  • 打赏
  • 举报
回复
补充一下,VC6的编译器不支持类外实现类元函数。所以象下面的代码在VC6中编译不过的,一定要类内实现!
#include <iostream>
using namespace std;

class Item
{
private:
char sex;
int height;
int weight;
public:
Item();
Item& setHeight( int iHeight);
Item& setWeight( int iWeight);
friend ostream& operator<<(ostream& c, Item& rhs);

};

Item::Item():sex('m'),height(180),weight(100){};
Item& Item::setHeight( int iHeight)
{
this->height = iHeight;
return *this;
}
Item& Item::setWeight( int iWeight)
{
this->weight = iWeight;
return *this;
}
ostream& operator<<(ostream& c, Item& rhs)
{
c << rhs.sex << "\t" << rhs.height << "\t" << rhs.weight << endl;
return c;
}


int main()
{
Item i;
cout << i << i;
i.setHeight(300);
i.setWeight(500);
Item j = *new Item();
cout << i << j;
return 0;
}
lin12345 2008-10-22
  • 打赏
  • 举报
回复
不能在类外用this
shuaiwang_01 2008-10-22
  • 打赏
  • 举报
回复
因为这个函数:
ostream& operator < < (ostream& c, Item& rhs)
{
c < < rhs.sex < < "\t" < < rhs.height < < "\t" < < rhs.weight < < endl;
return c;
}
在类外面调用了类中的私有成员,但此函数既不是成员函数也不是友元函数。
Fighting Horse 2008-10-22
  • 打赏
  • 举报
回复
operator << 不是类型成员函数啊
shuaiwang_01 2008-10-22
  • 打赏
  • 举报
回复
这样试试:
#include <iostream>
using namespace std;

class Item
{
private:
char sex;
int height;
int weight;
public:
Item();
Item& setHeight( int iHeight);
Item& setWeight( int iWeight);
friend ostream& operator << (ostream& , Item& );
};

Item::Item():sex('m'),height(180),weight(100){};
Item& Item::setHeight( int iHeight)
{
this->height = iHeight;
return *this;
}
Item& Item::setWeight( int iWeight)
{
this->weight = iWeight;
return *this;
}

ostream& operator << (ostream& c, Item& rhs)
{
c << rhs.sex << "\t" << rhs.height << "\t" << rhs.weight << endl;
return c;
}

int main()
{
Item i;
cout << i << i;
i.setHeight(300);
i.setWeight(500);
Item j = *new Item();
cout << i << j;
}

65,211

社区成员

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

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