CrossoverC 2016-06-03 02:18 采纳率: 33.3%
浏览 1861
已结题

将一个类的成员函数作为另外一个类的友元函数,使用VS2015报错

 #pragma once
#include <iostream>
#include <string>
#include <vector>
using namespace std;

class Screen {  

    friend void Window_mgr::clear(vector< Screen>::size_type);

public:
    typedef string::size_type pos;

    Screen() = default;
    Screen(pos ht, pos wd, char c) : cursor(0),
        height(ht), width(wd) { }
    char get()const
    {
        return contents[cursor];
    }
    inline char get(pos ht, pos wd)const;
    Screen &move(pos r, pos c);
    void some_memeber()const;
    Screen &set(char);
    Screen &set(pos, pos, char);
    Screen &display(ostream &os)
    {
        do_display(os);
        return *this;
    };
    const Screen &display(ostream &os)const
    {
        do_display(os);
        return *this;
    }
private:
    pos cursor = 0;
    pos height = 0, width = 0;
    string contents;
    mutable size_t access_ctr;
    void do_display(ostream &os)const { os << contents; }
};


inline Screen &Screen::move(pos r, pos c)
{
    pos row = r*width;
    cursor = row + c;
    return *this;

}

inline Screen &Screen::set(char c)
{
    contents[cursor] = c;
    return *this;


}
inline Screen &Screen::set(pos r, pos col, char ch)
{
    contents[r*width + col] = ch;
    return *this;
}


#pragma once
#include <iostream>
#include <vector>
#include <string>
#include "Screen.h"

class Screen;


using namespace std;

class Window_mgr
{

public:

    using ScreenIndex = vector< Screen>::size_type;
    void clear(ScreenIndex);


private:
    vector<Screen> screens{ Screen(20, 40, 'x') };

};


#include "Window_mgr.h"

class Screen;



void Window_mgr::clear(ScreenIndex i)
{

    Screen &s = screens[i];
    s.contents = string(s.height*s.width, ' ');
}

报错信息如下:
严重性 代码 说明 项目 文件 行
错误 C2248 “Screen::contents”: 无法访问 private 成员(在“Screen”类中声明) test0 c:\users\administrator\documents\visual studio 2015\projects\test0\test0\window_mgr.cpp 11
严重性 代码 说明 项目 文件 行
错误 C2248 “Screen::height”: 无法访问 private 成员(在“Screen”类中声明) test0 c:\users\administrator\documents\visual studio 2015\projects\test0\test0\window_mgr.cpp 11
严重性 代码 说明 项目 文件 行
错误 C2248 “Screen::width”: 无法访问 private 成员(在“Screen”类中声明) test0 c:\users\administrator\documents\visual studio 2015\projects\test0\test0\window_mgr.cpp 11
严重性 代码 说明 项目 文件 行
错误(活动) 后面有“::”的名称一定是类名或命名空间名 test0 c:\Users\Administrator\Documents\Visual Studio 2015\Projects\test0\test0\Screen.h 9
严重性 代码 说明 项目 文件 行
错误 C2027 使用了未定义类型“Window_mgr” test0 c:\users\administrator\documents\visual studio 2015\projects\test0\test0\screen.h 12

  • 写回答

1条回答 默认 最新

  • ZSGG_ACM 2016-06-03 07:14
    关注

    我记得类的成员函数不能作为友元函数

    评论

报告相同问题?

悬赏问题

  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名