CrossoverC 2016-05-27 01:12 采纳率: 33.3%
浏览 1295
已结题

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

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条回答

  • threenewbee 2016-05-27 01:16
    关注

    贴出你的代码,你如果有拷贝之类的操作但是没有定义拷贝构造函数,或者初始化对象,但是没有提供无参数公共构造函数等,都会报错。

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)