CrossoverC
2016-05-27 01:12关于c++ primer中一个友元的例子
20C++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条回答
为你推荐
- C++ primer 第三版中的一个小问题
- 智能指针
- 内存
- 对象
- c++
- string
- 4个回答
- 关于c++primer里泛型算法
- namespace
- 算法
- 泛型
- c++
- 1个回答
- c++ primer练习题疑问
- c++
- 1个回答
- 关于C++primer中顺序容器的一个疑问
- 4个回答
- C++PrimerPlus中的一道习题
- 5个回答
换一换