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 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题