标签: friend

C++ 声明模板参数阴影模板参数错误

我有以下 C++ 代码,我正在编译如下:

#include <cstddef>
#include <cassert>
template<typename T, std::size_t N> struct A;
template<typename T> struct Base {
    T &operator [](std::size_t i);
private:
    template<typename T, std::size_t N> friend struct A;
    #if !defined(NDEBUG)
        size_t n;
    #endif
};
template<typename T, std::size_t N> struct A : public Base<T> {
    A();
private:
    friend class Base<T>;
    T a[N];
};
template<typename T, std::size_t N> inline A<T,N>::A() { n = N; }
template<typename T> inline T& Base<T>::operator [](std::size_t i) {
    assert(i < n);
    return ((A<T,1>*)this)->a[i];
}
int …
Run Code Online (Sandbox Code Playgroud)

c++ templates friend

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

为什么 C++ 中不允许类型别名作为友元类名?

class A {    
};

class B {
    using C = A;

    // Compilation error: 
    // Type alias 'C' cannot be referenced with a class specifier 
    friend class C;    
};
Run Code Online (Sandbox Code Playgroud)

为什么 C++ 中不允许类型别名作为友元类名?

背后的原理是什么?

c++ types friend c++11 type-alias

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

运算符重载c ++

我试图在C++中预先形成运算符重载; 由于某种原因,编译继续给我错误

错误:'bool Matrix :: operator ==(const Matrix&,const Matrix&)'必须只有一个参数

现在,我知道有一些方法可以用一个参数使用它,但我明白通过使用朋友我可以这样做,但它仍然无法正常工作.

这是我的代码,

提前致谢.

class Matrix{
public:
 Matrix();
 friend bool operator==(Matrix &mtrx1,Matrix &mtrx2);
 friend bool operator!=(Matrix &mtrx1,Matrix &mtrx2);

protected:
 std::vector<Cell> _matrix;
 int _row;
 int _col;

};

inline bool Matrix::operator==(const Matrix& mtrx1, const Matrix& mtrx2){

/* .......... */
}
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading friend

0
推荐指数
2
解决办法
2160
查看次数

PHP好友添加系统无法正常工作

希望你能在这里帮助我,我已经整天试着把这个问题搞清楚了.

我有一个朋友系统和一些用户,他们可以互相添加,并且工作完美,但问题是他们可以互相添加多一次..

继承我的脚本,检查他们是否已经是朋友..

但它不起作用:(

<?
// connect Databasen
include('connect.php');
//hvis get bruger ikke er tom
if (!empty($_GET["bruger"])) {
//tjeker om venner er anøsgt i forvejen
$bruger = $_SESSION['username'];
$ven = $_GET["bruger"];
$tjek = mysql_query("SELECT * FROM venner WHERE username='$bruger' AND ven='$ven'");
if ($tjek) {
//indsæt
mysql_query("INSERT INTO venner VALUES('','$bruger','$ven','','nej')");
echo "venskab er ansøgt";
exit;
}
else {
echo "Du har allerede ansøgt";
}
}
?>
Run Code Online (Sandbox Code Playgroud)

php mysql system friend

0
推荐指数
1
解决办法
367
查看次数

朋友操作员行为(const与非const)

我有以下C++代码:

#include <iostream>

template <class T>
void assign(T& t1, T& t2) {
   std::cout << "First method" << std::endl;
   t1 = t2;
}

template <class T>
void assign(T& t1, const T& t2) {
   std::cout << "Second method" << std::endl;
   t1 = t2;
}

class A {
   public:
      A(int a) : _a(a) {};
   private:
      int _a;
      friend A operator+(const A& l, const A& r);
};

A operator+(const A& l, const A& r) {
   return A(l._a + r._a);
}

int main() {
  A …
Run Code Online (Sandbox Code Playgroud)

c++ friend

0
推荐指数
1
解决办法
407
查看次数

无法访问朋友ostream中的私人成员

我试着做朋友的ostream功能.编译器说我无法访问该类的私有成员,即使我声明它friend.我读了一个类似的问题,它说问题出在namespcaes上.(问题:C++友好函数无法访问私有成员)

我的代码如下:

标题:

#include <iostream>
#include <string>
//using namespace std;

namespace biumath
{
#ifndef BIUMATH_H
#define BIUMATH_H
class Assignment
{
private:
    int **m_varArray;
    int m_rowsOfVarArray;
public:
     Assignment(); //1
     Assignment(char symbol, double value); //2
     bool containsValueFor(char symbol) const; //3
     double valueOf(char symbol) const; //4
     void add(char symbol, double val); //5
     friend std::ostream& operator<< (std::ostream& out, 
     const Assignment& assignment);
};
}
#endif
Run Code Online (Sandbox Code Playgroud)

CPP:

#include <iostream>
#include "biumath.h"
using namespace biumath;
using std::cout;
using std::endl;

ostream& operator<< …
Run Code Online (Sandbox Code Playgroud)

c++ iostream namespaces friend

0
推荐指数
1
解决办法
1367
查看次数

定义ostream运算符的Friend函数

我想定义一个ostream运算符,以方便我输出type的变量alglib::complex。为了提供一个不包含alglib库的有效示例,我将重载complex<double>下面的输出(由于问题的早期版本,因此进行了澄清)。在头文件“ my_class.h”中,我有

using namespace std;
#include <complex>
#include <iostream>

class my_class {

    public:

    ostream& operator << (std::ostream& os, complex<double> a) {
        os << "(" << real(a) << "," << imag(a) << ")";
        return os;
    }

    void output(complex<double>);

    my_class() {}
    ~my_class() {}
};
Run Code Online (Sandbox Code Playgroud)

在源文件“ my_class.cpp”中,

#include "my_class.h"

void my_class::output(complex<double> cd) {
    cout << cd << endl;
}
Run Code Online (Sandbox Code Playgroud)

最后,我有一个主要的方法文件“ run_my_class.cpp”:

#include "my_class.h"

int main(int argc, const char* argv[]) {

    my_class obj;
    complex<double> cd=complex<double>(1.0,-1.0);
    obj.output(cd);

}
Run Code Online (Sandbox Code Playgroud)

我尝试使用编译

g++ -c …
Run Code Online (Sandbox Code Playgroud)

c++ class operators friend ostream

0
推荐指数
1
解决办法
1401
查看次数

函数在类范围外声明但不是朋友.这是如何运作的?

我有以下课程:

class PhoneCall{
private:
    string number;
public:
    /*some code here */
};
Run Code Online (Sandbox Code Playgroud)

现在,我宣布一个函数(不是朋友PhoneCall,做一些具体的操作和返回)PhoneCall对象

PhoneCall callOperation()
Run Code Online (Sandbox Code Playgroud)

另一个将PhoneCall对象作为参数

void userCall(PhoneCall obj)
Run Code Online (Sandbox Code Playgroud)

我期待它不起作用,除非明确宣布为该类的朋友.

即使他们不是PhoneCall类的朋友,这些功能为什么以及如何工作?

类的友元函数在类的范围之外定义,但它有权访问类的所有私有和受保护成员 http://www.tutorialspoint.com/cplusplus/cpp_friend_functions.htm

c++ function friend

0
推荐指数
1
解决办法
290
查看次数

朋友功能无法访问私人会员

我正在尝试获取朋友的功能class1ships访问两者的私有成员,但它表示这些成员无法访问.

代码如下,问题在于ships.cpp.我试图在一个文件中以更简单的方式重现这个问题,但它没有发生在那里,所以我不知道这里有什么问题.也许这是一个循环声明问题?

ships.h

#ifndef  _SHIPS_H_
#define _SHIPS_H_

#include "point.h"

class class1;

class Ships{
public:
    friend char* checkpoints();
private:
    Point ship[6]; 
};
#endif // ! _SHIPS_H_
Run Code Online (Sandbox Code Playgroud)

ships.cpp

#include "ships.h"
#include "class1.h"

char* checkpoints(Ships ship, class1 game) {

    ship.ship[0];//cannot access private member declared in class 'Ships'
    game.smallship;//cannot access private member declared in class 'class1'

    return nullptr;
}
Run Code Online (Sandbox Code Playgroud)

class1.h

#ifndef _CLASS1_H_
#define _CLASS1_H_

#include  "ships.h"
class class1 {
public:
    friend char* checkpoints();
private:
    static const int LIVES …
Run Code Online (Sandbox Code Playgroud)

c++ private friend c++11

0
推荐指数
1
解决办法
1016
查看次数

如何将一个类的构造函数的调用限制为仅其工厂类?

我想防止除类的工厂之外的任何东西构造类类型的对象。我需要该类具有一个公共接口,但我想将其创建仅限于其工厂。我怎么做?

让我们称之为班级Car和工厂CarFactory。这是我在不使用friend所有私有成员并将其暴露给工厂的情况下如何执行此操作的最初想法:

class Car {
private:
  Car();
  Car(Car& ref);

  friend class CarFactory;
};

class CarFactory {
public:
  Car * makeCar();
};
Run Code Online (Sandbox Code Playgroud)

我发现了一个与Java有关的问题:如何使构造函数仅对工厂类可用?

上面的代码按原样工作。澄清一下,我想知道是否有一种方法可以只与工厂共享构造函数,而不能与所有私有成员共享?

c++ factory friend

0
推荐指数
1
解决办法
428
查看次数