其他分享
首页 > 其他分享> > c – 朋友功能 – 声明顺序

c – 朋友功能 – 声明顺序

作者:互联网

我有两个名为Screen和Window_mgr的类.

Screen允许Window_mgr通过friend函数声明修改其私有/受保护成员.

因此,Window_mgr在代码的最后定义了一个名为Window_mgr :: clear的非成员函数,该函数应该使用它.

不幸的是,我得到了一些荒谬的错误,我无法解释.

我错过了什么?

enter image description here

Screen.h

#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

解决方法:

您不能在Window_mgr类中声明Screen对象的向量,因为代码中的那个点编译器不知道Screen.如果要声明一个指针向量,可以通过前向声明屏幕来修复它,但对于实际对象的向量,必须有完整定义.

您需要切换Window_mgr和Screen的顺序,并向Window_mgr类声明友谊:

class Screen {
public:
    friend class Window_mgr;
    ...
};
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, ' ') };
};

why does the compiler know Window_mgr but not Window_mgr::ScreenIndex

C对友谊声明中使用的类名有一个特殊规则:

If the name of the class that is used in the friend declaration is not yet declared, it is forward declared on the spot.

这就是编译器“知道”Window_mgr的方式(即它没有;它接受你的话).对于在“befriended”类中声明的成员函数或成员类型,没有这样的规则.这就是编译器不知道Window_mgr :: ScreenIndex的原因.

标签:friend-function,c,header-files
来源: https://codeday.me/bug/20190829/1760740.html