标签: friend-function

如何解决"声明朋友时必须使用的类"错误?

class two;
class one
{
    int a;
    public:
        one()
        {
            a = 8;
        }
    friend two;
};

class two
{
    public:
        two() { }
        two(one i)
        {
            cout << i.a;
        }
};

int main()
{
    one o;
    two t(o);
    getch();
}
Run Code Online (Sandbox Code Playgroud)

我从Dev-C++得到这个错误:

a class-key must be used when declaring a friend
Run Code Online (Sandbox Code Playgroud)

但是使用Microsoft Visual C++编译器编译时运行正常.

c++ dev-c++ visual-c++ friend-function

4
推荐指数
2
解决办法
2812
查看次数

重载operator <<以输出对象成员而不使用友元函数

经过长时间的差距,我正在刷新cpp,试图理解运算符重载方法.我试图重载"operator <<"来输出对象的成员.但是如果不使用朋友功能,我无法这样做.我正在寻找一种不使用友元功能的方法.

这是我的班级def:

class Add{
private:
int x;

public:
friend ostream& operator<<(ostream& ostr, Add const& rhs); //Method 1
void operator<<(ostream& ostr);                //Method 2
};
Run Code Online (Sandbox Code Playgroud)

功能实现

//Method 1
ostream& operator<<(ostream &ostr, Add const& rhs)
{
    ostr<<rhs.x;

return ostr;
}

//Method 2
void Add::operator<<(ostream& ostr)
{
    cout<<" using operator<< \n";
    ostr<<x;
}
Run Code Online (Sandbox Code Playgroud)

来自main函数的调用

cout<<Obj_Add;  //calls the Method 1

Obj_Add<<cout;  //calls the Method 2
Run Code Online (Sandbox Code Playgroud)

现在我的问题是,我想在不使用friend函数的情况下实现方法1类型调用.但是不知道,cpp是否有可能.我尝试了很少的实现,但都给了我编译错误.请帮助我理解我在这里缺少的观点.

c++ operator-overloading friend-function

4
推荐指数
1
解决办法
7915
查看次数

来自多个班级的朋友模板功能

我有这个代码:

template<typename T> T f() {
// ...
}

class A {
    friend A f();
};

class B {
    friend B f();
};
Run Code Online (Sandbox Code Playgroud)

我收到ambiguating new declaration of ‘B f()’错误.

但是,如果我将我的代码更改为以下

template<typename T> void f(T arg) {
// ...
}

class A {
    friend void f(A);
};

class B {
    friend void f(B);
};
Run Code Online (Sandbox Code Playgroud)

程序编译精细.

有人可以帮我弄清问题是什么?

c++ oop function friend friend-function

4
推荐指数
1
解决办法
315
查看次数

友情功能如何在内部实施

我们都在C++中使用了全局级别以及类级别的友元函数.我试图在互联网上搜索内部朋友功能是如何实现的.

"friend"关键字做了什么操作.例如,我们知道如何在内部实现v-ptr和v-table,我正在寻找同样的答案.

请注意: 这个问题与朋友功能或朋友功能的使用无关.

c++ compiler-construction friend-function

3
推荐指数
1
解决办法
320
查看次数

在课堂上和外面定义的朋友之间的区别:错误还是功能?

考虑:

struct Y {
  Y(float f) : f(f) {}
  float f;
};

struct X {
  X(Y y) : i(y.f) {}
  int i;
  friend bool operator==(X x1, X x2) {
    return x1.i == x2.i;
  }
};

int main()
{
    return Y(1) == Y(2); // ERROR
}
Run Code Online (Sandbox Code Playgroud)

这会导致MSVC上的以下错误和Clang上的类似错误:

'==': candidate function(s) not accessible
could be the friend function at '..\main.cpp(11)' : '=='  [may be found via argument-dependent lookup]
Run Code Online (Sandbox Code Playgroud)

如果我将友元函数的定义移出类:

struct X {
    X(Y y) : i(y.f) {}
    int i;
    friend bool operator==(X …
Run Code Online (Sandbox Code Playgroud)

c++ friend-function language-lawyer

3
推荐指数
1
解决办法
67
查看次数

朋友功能 - 声明顺序

我有两个叫做Screen和的 课程Window_mgr.

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

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

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

我错过了什么?

在此输入图像描述

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;

    // …
Run Code Online (Sandbox Code Playgroud)

c++ header-files friend-function

3
推荐指数
1
解决办法
284
查看次数

朋友定义的函数的命名空间是什么?

如果我在一个类中定义了一个函数.该函数的命名空间是什么?

namespace A{namespace B{
    struct S{
        friend void f(S const&){};
    };
}}

int main(){
    A::B::S s{};
    f(s); // ok
    ::f(s); // not ok, no f in global namespace
    A::B::f(s); // no f in A::B
    A::B::S::f(s); // not ok, no f in A::B::S
}
Run Code Online (Sandbox Code Playgroud)

它甚至有名称空间吗?拥有命名空间是否有意义?

如果我想消除常用习语中的呼叫,该怎么办?

using std::swap;
swap(a, b);
Run Code Online (Sandbox Code Playgroud)

我是否被迫在课外定义并宣布为朋友?

c++ overloading namespaces friend-function c++11

3
推荐指数
1
解决办法
86
查看次数

为什么 Friend 函数不能访问类的私有成员

class A{
public:
    void printer(){
        B obj;
        obj.private_data = 10; // <- fails, says member inaccessible
    }
}
class B{
friend void A::printer();
private:
    int private_data;
}
Run Code Online (Sandbox Code Playgroud)

打印机功能是否可以访问 B 类的私有成员?我试图将 B 的 obj 作为 arg 传递给打印机,但它仍然失败

c++ class friend-function

3
推荐指数
1
解决办法
68
查看次数

类内定义的友元函数的调试标志中的命名空间

我正在处理一个类,该类在没有外部声明的情况下在类中定义了友元函数

namespace our_namespace {
template <typename T>
struct our_container {

  friend our_container set_union(our_container const &, our_container const &) {
    // meaningless for the example here, just a valid definition
    // no valid semantics
    return our_container{};
  }
};
}  // namespace our_namespace
Run Code Online (Sandbox Code Playgroud)

正如所讨论的(例如此处此处),函数set_union不在our_namespace命名空间中,但可以通过参数依赖查找找到:

auto foo(std::vector<our_namespace::our_container<float>> in) {
  // works:
  return set_union(in[0], in[1]);
}
Run Code Online (Sandbox Code Playgroud)

但是我注意到,调试标志中 set_union似乎是在our_namespace命名空间中

        mov     rdi, qword ptr [rbp - 40] # 8-byte Reload
        mov     rsi, rax
        call …
Run Code Online (Sandbox Code Playgroud)

c++ friend-function argument-dependent-lookup

2
推荐指数
1
解决办法
39
查看次数

在模板化类之外定义非模板函数

我正在学习模板化类的非模板友元函数和模板友元函数。所以我尝试了下面的代码:

#include <iostream>   


template<typename T>
class  cl
{
private :
    T val;
public:
    cl()= default;
    explicit cl(T v) : val(std::move(v)) {}    

    friend void non_template_friend(cl m);
};

template <typename T>
void non_template_friend(cl<T> m)  { std::cout << m.val << std::endl;}


int main()
{

    cl<int> c(10);
    non_template_friend(c);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

因此,当我编译时,我得到了:undefined reference tonon_template_friend(cl)' ` 所以要解决这个问题,我必须像这样在类定义中移动友元函数定义:

template<typename T>
class  cl
{
private :
    T val;
public:
    cl()= default;
    explicit cl(T v) : val(std::move(v)) {}    

    friend void non_template_friend(cl m) { std::cout << m.val …
Run Code Online (Sandbox Code Playgroud)

c++ templates friend-function template-classes

2
推荐指数
1
解决办法
214
查看次数