关于typedef和友元的前向声明

shienaiwun 2010-11-12 10:31:31
以下是c++ prime (第四版)中397页的友元和typedef的例子

#include <iostream>
class Screen;
class Window_Mgr;
using std::string;
using namespace std;
typedef std::string::size_type Screen::index;
class Window_Mgr
{
public :
void relocate(Screen::index t )/*报错 ,没有定义Screen::index,而如果把Screen定义放到之前,Screen里面的友元定义又有问题*/
{
}
};
class Screen
{
public :
typedef std::string::size_type index;
friend void Window_Mgr::relocate(Screen::index t);//Window_Mgr必须在之前定义

private:
std::string contents;
index cursor;
index height,width;
};

int main()
{
return 0;
}

//在这里,如果把Screen的定义放到Window_Mgr前面,又不符合友元的声明顺序,不知道怎么解开这个锁
...全文
92 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq120848369 2010-11-12
  • 打赏
  • 举报
回复
为了一个明知故问的类型纠结,没意思.
qq120848369 2010-11-12
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
using namespace std;

namespace Index
{
typedef string::size_type index;
};

using namespace Index;

class Window_Mgr
{
public :
void relocate(index)
{
}
};

class Screen
{
public :

friend void Window_Mgr::relocate(index t);

private:
string contents;
index cursor;
index height,width;
};

int main()
{
return 0;
}
失落的凡凡 2010-11-12
  • 打赏
  • 举报
回复
互相引用到对方的内容……貌似没有办法……
jefferyyangkai 2010-11-12
  • 打赏
  • 举报
回复
vc6.0没有报错啊?
不过也许你的环境下void relocate(Screen::index t )后面不加{},在class Screen
之后在定义会可以

64,646

社区成员

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

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