У меня есть два класса с именами Screen
и Window_mgr
.
Screen
позволяет Window_mgr
изменять свои частные/защищенные члены через объявление функции друга.
В результате Window_mgr
определяет в самом конце кода не являющуюся членом функцию с именем Window_mgr::clear
, которая должна ее использовать.
К сожалению, я получаю некоторые нелепые ошибки, которые я не могу объяснить сам.
Что мне не хватает?
Экран.ч
#pragma once
#ifndef SCREEN_H
#define SCREEN_H
#include <string>
#include <vector>
class Window_mgr {
public:
// location ID for each screen on the window
using ScreenIndex = std::vector<Screen>::size_type;
// reset the Screen at the given position to all blanks
void clear(ScreenIndex);
private:
std::vector<Screen> screens{ Screen(24, 80, ' ') };
};
class Screen {
public:
// Friends
friend void Window_mgr::clear(ScreenIndex);
//friend class Window_mgr;
// Fields
// typedef => creates an alias
// typedef std::string::size_type pos;
// alternative way to declare a type member using a type alias
using pos = std::string::size_type;
// Constructors
Screen() = default; // needed because Screen has another constructor
// cursor initialized to 0 by its in-class initializer
Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht * wd, c) {} // get the character at the cursor
Screen &display(std::ostream &os) // function is in the class body => implicitly inline
{
do_display(os);
return *this;
}
const Screen &display(std::ostream &os) const // function is in the class body => implicitly inline
{
do_display(os);
return *this;
}
// Methods
char get() const { return contents[cursor]; } // implicitly inline
inline char get(pos ht, pos wd) const; // explicitly inline
Screen &move(pos r, pos c); // can be made inline later
Screen &set(char);
Screen &set(pos, pos, char);
private:
// Fields
mutable size_t access_ctr;
pos cursor = 0;
pos height = 0, width = 0;
std::string contents;
// Methods
void do_display(std::ostream &os) const { os << contents; }
};
inline Screen &Screen::set(char c)
{
contents[cursor] = c; // set the new value at the current cursor location
return *this; // return this object as an lvalue
}
inline Screen &Screen::set(pos r, pos col, char ch)
{
contents[r*width + col] = ch; // set specified location to given value
return *this; // return this object as an lvalue
}
// we can specify inline on the definition
inline Screen &Screen::move(pos r, pos c) {
pos row = r * width; // compute the row location
cursor = row + c; // move cursor to the column within that row
return *this; // return this object as an lvalue
}
char Screen::get(pos r, pos c) const // declared as inline in the class
{
pos row = r * width; // compute row location
return contents[row + c]; // return character at the given column
}
void Window_mgr::clear(ScreenIndex i)
{
// s is a reference to the Screen we want to clear
Screen &s = screens[i];
// reset the contents of that Screen to all blanks
s.contents = string(s.height * s.width, ' ');
}
#endif
#include <vector>
и#include <iostream>
? - person Sergey Kalinichenko   schedule 19.01.2016class Screen;
для объявленияScreen
в начало файла, но это не решает проблему полностью. - person HansMusterWhatElse   schedule 19.01.2016class Screen;
вверху решит эту проблему, если вы захотите выполнитьvector<Screen*>
, но дляvector<Screen>
должен быть доступен полныйScreen
. - person Sergey Kalinichenko   schedule 19.01.2016