返回信息流在两个文件中分别写两个类Screen,WindowMgr然后把WindowMgr的函数displayWindow设置为Screen的友元怎么弄呢?
#pragma once
#include <string>
using namespace std;
class Screen
{
public:
typedef string::size_type index;
friend void WindowMgr::displayWindow(Screen& sr);
Screen(void);
~Screen(void);
private:
string contents;
index cursor;
index height, width;
};
总是报错error C2653: 'WindowMgr' : is not a class or namespace name
即使在classScreen的前面加上WindowMgr的前向声明也没有用,能不能告诉一下怎么弄?谢谢!
这是一条镜像帖。来源:北邮人论坛 / cpp / #37991同步于 2010/4/15
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
C++的友元问题
bohou
2010/4/15镜像同步5 回复
订阅后,新回复会通过你的通知中心匿名送达。
5 条回复
【 在 bohou 的大作中提到: 】
: 在两个文件中分别写两个类Screen,WindowMgr然后把WindowMgr的函数displayWindow设置为Screen的友元怎么弄呢?
: #pragma once
: #include <string>
: ...................
WindowMgr必须在screen前面定义,因为你调用了它的成员函数,前向声明没用。
ps:好像是c++ primer 上的范例程序啊。。。。上面写的很清楚
【 在 a206206 的大作中提到: 】
:
: WindowMgr必须在screen前面定义,因为你调用了它的成员函数,前向声明没用。
: ps:好像是c++ primer 上的范例程序啊。。。。上面写的很清楚
: ...................
呵呵,是c++ primer上的程序,但没看懂他怎么弄的,能麻烦一下高速我解决的办法么?谢谢!
他不是告诉你了吗 在screen之前定义
【 在 bohou (bohou) 的大作中提到: 】
: 呵呵,是c++ primer上的程序,但没看懂他怎么弄的,能麻烦一下高速我解决的办法么?谢谢!
#include <string>
class Screen; //前向声明是必要的 因为WindowMgr类中的displayWindow参数类型是Screen&
class WindowMgr
{
public: // public是必要的 因为Screen中要通过WindowMgr类将displayWindow声明为友元
// 如果这里是private 则在Screen中把WindowMgr类声明为友元一样可以通过编译
void displayWindow(Screen& sr);
};
using std::string;
class Screen
{
public:
typedef string::size_type index;
friend void WindowMgr::displayWindow(Screen& sr);
Screen(void);
~Screen(void);
private:
string contents;
index cursor;
index height, width;
};
以上代码在 VS2005 下编译通过