CrossoverC
CrossoverC
2016-05-27 01:12

关于c++ primer中一个友元的例子

20
  • c++

C++primer中的一个例子,关于友元,在VS2015报错,定义了两个类Screen和window-mgr,window-mgr作为Screen的友元,但是提示Screen没有构造函数
手机传不上图片,代码如下:

 #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') };//报错,提示Screen类没有构造函数

};

#include <iostream>
#include <string>
#include "Window_mgr.h"

using namespace std;

class Screen {
    friend class Window_mgr;
public:
    typedef string::size_type pos;
    Screen(pos ht, pos wd, char c) :height(ht), width(wd), contents(ht*wd, c) {}
    Screen() = default;

    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;
}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答

为你推荐

换一换