定义了两个类Screen和window-mgr,window-mgr作为Screen的友元,但是提示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;
}
#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类没有构造函数**
};
我没看错的话,你是在两个头文件互相包含并且没有用#ifndef之类的防止重复包含的措施?
包含头文件的意思是把整个头文件复制到 #include这一行,你这种情况不,A包含B,B包含A,A又包含B,不就是无限循环了么。。
只是声明友元,没必要包含头文件,加上#ifndef之类的句子,然后直接注释掉#include "Window_mgr.h" 这句再试试
另外,类的成员提供默认值(Window_mgr里的screens)这一点,因为没用过VS2015,不知道是不是支持,至少VS2015之前的任何版本(比如VS2013)都不支持。
Screen 类的 Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht * wd, c) {} 就是您准备的构造函数吧;
pos是什么数据类型?会不会是编译器认为 Screen(int Height, int Width, char C)才符合要求??