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

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

这个其实跟友元没关系。就是你类的设计有问题。你贴下代码吧,这样才可以更快找出问题所在。光猜也不好说。

友元跟构造函数没什么关系,每个类都必须要有构造函数,你那个类是不是没有构造函数