我有一个带有私有构造函数和析构函数的模板类:
template <typename T>
class Client
{
private:
Client(...) {}
~Client() {}
template <typename U>
friend class Client<T>& initialize(...);
};
template <typename T>
Client<T> initialize(...)
{
Client<T> client = new Client<T>(...);
}
Run Code Online (Sandbox Code Playgroud)
我不确定朋友的正确语法。有人可以帮忙吗?
首先提供一些上下文,这是针对涉及信号量的赋值。我们要找到哲学家就餐问题的代码,让它发挥作用,然后进行一些分析和操作。但是,我遇到了一个错误。
原始代码取自http://www.math-cs.gordon.edu/courses/cs322/projects/p2/dp/ ,使用C++解决方案。
我在 Code::Blocks 中收到的错误是
philosopher.cpp|206|error: 'Philosopher_run' was not declared in this scope|
Run Code Online (Sandbox Code Playgroud)
并且此错误发生在该行中:
if ( pthread_create( &_id, NULL, (void *(*)(void *)) &Philosopher_run,
this ) != 0 )
Run Code Online (Sandbox Code Playgroud)
我查找了 pthread_create 方法,但无法修复此错误。如果有人可以向我解释如何纠正这个错误,以及为什么会发生这个错误,我将不胜感激。我试图仅提供相关代码。
class Philosopher
{
private:
pthread_t _id;
int _number;
int _timeToLive;
public:
Philosopher( void ) { _number = -1; _timeToLive = 0; };
Philosopher( int n, int t ) { _number = n; _timeToLive = t; };
~Philosopher( void ) {};
void getChopsticks( void );
void releaseChopsticks( void …Run Code Online (Sandbox Code Playgroud) 假设我的 Add.h 位于命名空间内,并且我将其设为 AddTest 的友元,以便它可以访问 AddTwoNumber。
namespace mynamespace
{
class Add
{
friend class AddTest;
public:
Add(){};
~Add(){};
private:
int AddTwoNumber(const int a, const int b){return a+b};
};
}
Run Code Online (Sandbox Code Playgroud)
我的 AddTest.h 是
#include "Add.h"
#include "gtest/gtest.h"
class AddTest : public ::testing::Test
{
protected:
AddTest(){};
virtual ~AddTest(){};
virtual void SetUp()
{
mynamespace::Add addobj;
result = addobj.AddTwoNumber(2, 3);
};
virtual void TearDown(){};
int result;
};
Run Code Online (Sandbox Code Playgroud)
但是,它返回错误,因为 AddTwoNumber 是私有的。如果我在 Add.h 中取出“mynamespace”,该代码就可以工作。有没有办法保留命名空间但仍允许 AddTest 访问 Add.h 中的私有方法?
所以我有一个class内部foo命名空间,其中包含一个friend函数。现在我希望函数的定义friend位于不同的命名空间中bar,以便可以按照下面看到的方式调用它。我得到的错误是val无法访问私有成员。
问:为什么?
#include <iostream>
namespace foo
{
template<typename T>
class myclass
{
private:
T val;
public:
myclass(T v) : val(v) {}
template<class U>
friend void myfun(myclass<U>);
};
namespace bar
{
template<class U>
void myfun(myclass<U> a)
{
std::cout << a.val;
}
} //bar
} //foo
int main()
{
foo::myclass<int> a(5);
foo::bar::myfun(a);
}
Run Code Online (Sandbox Code Playgroud) 最近我一直在研究一个库来处理数千个数字的非常大的数字.我现在已经为这些事情开发了一个阶乘函数,因为我只是设置了乘法.
largeNum factorial(largeNum& input) {
if (input > one) return (input * factorial(--input));
else return one;
}
Run Code Online (Sandbox Code Playgroud)
"one"是一个largeNum被定义为具有"一"的值,带有"+"符号,因为我还没有实现整数转换.
Factorial是largeNum类的友元函数.我没有得到任何语法错误,它必须是合乎逻辑的.
前缀 - 运算符已正确重载并进行了测试.
乘法和">"运算符也是如此.
也许我只是因为我有点睡眠不足而失明,但我需要一些帮助.干杯.
我有以下 C++ 片段代码。在main()函数内部声明一个类。
我们不能在本地类中定义友元函数是什么原因?
#include<iostream>
int main()
{
class Foo
{
void foo() {} // Ok
friend void Bar(){}; // Error
};
}
Run Code Online (Sandbox Code Playgroud) 我在头文件的类中有两个友元函数
它们都在类中声明
但问题是当我将它们的主体写入 .cpp 文件时,我收到此错误
undefined reference to...
当我在 .h 文件中写入正文时,出现此错误
multiple definition of..
所以我不明白我应该在哪里写友元函数的主体?
考虑以下代码:
namespace A
{
class B
{
protected:
friend class C;
static void foo();
};
}
class C
{
public:
C() { A::B::foo(); }
};
int main()
{
C c;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
按照当前构造,此代码将无法编译 - 中 声明的友谊class B适用于 a (当前不存在)A::C,而不是C全局命名空间中的 the 。假设我无法添加C到非全局命名空间,我如何有效地解决这个问题?我尝试过使用friend class ::C;,但编译器不喜欢这样。我也尝试过在范围class C;之前进行向前声明namespace A,但这似乎也不起作用。
可能重复:
你何时应该在C++中使用'friend'?
我看到很多人推荐一个函数/类在这里成为另一个类的朋友,尽管还有其他选择.难道不应该在C++中谨慎使用朋友吗?在决定使用好友功能之前,我觉得必须考虑其他选项.欢迎提出意见/建议.
我有一个简单的C++类,我正在尝试添加流操作符,因此它可以与cout和一起使用cin
#include <iostream>
namespace testing_namespace {
class test {
friend std::ostream &operator<<(std::ostream &os, const test &o);
friend std::istream &operator>>(std::istream &is, test &o);
public:
void doThing();
private:
int member;
};
}
Run Code Online (Sandbox Code Playgroud)
这是实现文件:
std::ostream &operator<<(std::ostream &os, const testing_namespace::test &o) {
return os << o.member;
}
std::istream &operator>>(std::istream &is, testing_namespace::test &o) {
return is >> o.member;
}
void testing_namespace::test::doThing() {
std::cout << member << " thing" << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我在编译时遇到错误:
In function 'std::ostream& operator<<(std::ostream&, const testing_namespace::test&)':
test.cpp:8:20: error: 'int testing_namespace::test::member' …Run Code Online (Sandbox Code Playgroud) c++ ×10
friend ×10
class ×3
namespaces ×3
templates ×2
c++11 ×1
c++17 ×1
factorial ×1
googletest ×1
header ×1
local-class ×1
math ×1
scope ×1