C++
我有一个主类mainclass
,他的私人成员变量应为朋友类friendsclass
可见。 两者均由int
称为dim
的模板,并且它们都具有各自的标头和源文件(因此在不同的翻译单元中)。 由于mainClass
并不真正取决于friendsclass
(并且要避免循环依赖性)i转发声明friendsclass
在执行朋友声明时在MainClass
中。 此外,在源文件的末尾,我明确地将两个类实例化dim = 2
和dim = 3
。 但是,当我编译时,我会遇到一个错误,即mainClass
的私有成员变量在此上下文中是私有的,当我尝试以friendsclass
的方法使用它。 我怀疑这与friendsclass
的特定实例化有关的事实没有意识到MainClass
的相应实例化已将其宣布为朋友,但我不是确保如何解决问题。 代码:
// MainClass.hpp
#ifndef MAIN_CLASS_HPP
#define MAIN_CLASS_HPP
template <int dim>
class MainClass
{
public:
MainClass(){};
private:
template <int friend_dim>
class FriendClass;
friend class FriendClass<dim>;
double private_member = 3.0;
};
#endif
// MainClass.cpp
#include "MainClass.hpp"
template class MainClass<2>;
template class MainClass<3>;
// FriendClass.hpp
#ifndef FRIEND_CLASS_CPP
#define FRIEND_CLASS_CPP
#include "MainClass.hpp"
template <int dim>
class FriendClass
{
public:
FriendClass(){};
void print_main_class(MainClass<dim> &main_class);
};
#endif
// FriendClass.cpp
#include "FriendClass.hpp"
#include <iostream>
template <int dim>
void FriendClass<dim>::print_main_class(MainClass<dim> &main_class)
{
std::cout << main_class.private_member << std::endl;
}
template class FriendClass<2>;
template class FriendClass<3>;
// main.cpp
#include "MainClass.hpp"
#include "FriendClass.hpp"
int main()
{
const int dim = 2;
MainClass<dim> main_class;
FriendClass<dim> friend_class;
friend_class.print_main_class(main_class);
return 0;
}
可以在
I have a main class MainClass
whose private member variables should be visible to a friend class FriendClass
.
Both are templated by an int
called dim
, and they both have their respective header and source files (and are thus in different translation units).
Since MainClass
doesn't really depend on FriendClass
(and to avoid circular dependency) I forward declare FriendClass
as a template class when doing the friend declaration in MainClass
.
Additionally, at the end of the source files I explicitly instantiate both classes for dim = 2
and dim = 3
.
However, when I compile I get an error that the private member variable of MainClass
is private within this context when I try to use it in a method of FriendClass
.
I suspect this has something to do with the fact that a particular instantiation of FriendClass
does not recognize that the corresponding instantiation of MainClass
has declared it a friend, but I'm not sure how to fix the problem.
Code:
// MainClass.hpp
#ifndef MAIN_CLASS_HPP
#define MAIN_CLASS_HPP
template <int dim>
class MainClass
{
public:
MainClass(){};
private:
template <int friend_dim>
class FriendClass;
friend class FriendClass<dim>;
double private_member = 3.0;
};
#endif
// MainClass.cpp
#include "MainClass.hpp"
template class MainClass<2>;
template class MainClass<3>;
// FriendClass.hpp
#ifndef FRIEND_CLASS_CPP
#define FRIEND_CLASS_CPP
#include "MainClass.hpp"
template <int dim>
class FriendClass
{
public:
FriendClass(){};
void print_main_class(MainClass<dim> &main_class);
};
#endif
// FriendClass.cpp
#include "FriendClass.hpp"
#include <iostream>
template <int dim>
void FriendClass<dim>::print_main_class(MainClass<dim> &main_class)
{
std::cout << main_class.private_member << std::endl;
}
template class FriendClass<2>;
template class FriendClass<3>;
// main.cpp
#include "MainClass.hpp"
#include "FriendClass.hpp"
int main()
{
const int dim = 2;
MainClass<dim> main_class;
FriendClass<dim> friend_class;
friend_class.print_main_class(main_class);
return 0;
}
Code available to compile live at onlinegdb.com
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已声明了两个不同的类模板,均名为
friendclass
。可能是无意的。一个是全局
friendclass
,另一个是mainclass :: friendclass
。您可以通过在其存在的名称空间中向前声明类模板来解决此问题。
You have declared two different class templates both named
FriendClass
. Probably unintentionally.One is the global
FriendClass
and the other isMainClass::FriendClass
.You can fix this by forward declaring your class template in the namespace it exists in.