C++

发布于 2025-02-12 11:32:17 字数 2144 浏览 2 评论 0原文

我有一个主类mainclass,他的私人成员变量应为朋友类friendsclass可见。 两者均由int称为dim的模板,并且它们都具有各自的标头和源文件(因此在不同的翻译单元中)。 由于mainClass并不真正取决于friendsclass(并且要避免循环依赖性)i转发声明friendsclass在执行朋友声明时在MainClass中。 此外,在源文件的末尾,我明确地将两个类实例化dim = 2dim = 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟燃烟灭 2025-02-19 11:32:17

您已声明了两个不同的类模板,均名为friendclass。可能是无意的。

一个是全局friendclass,另一个是mainclass :: friendclass

您可以通过在其存在的名称空间中向前声明类模板来解决此问题。

// MainClass.hpp
#ifndef MAIN_CLASS_HPP
#define MAIN_CLASS_HPP

template <int>
class FriendClass;

template <int dim>
class MainClass
{
public:
    MainClass(){};
private:
    
    friend FriendClass<dim>;
//  Now the global FriendClass is a friend.
    
    double private_member = 3.0;
};

#endif

You have declared two different class templates both named FriendClass. Probably unintentionally.

One is the global FriendClass and the other is MainClass::FriendClass.

You can fix this by forward declaring your class template in the namespace it exists in.

// MainClass.hpp
#ifndef MAIN_CLASS_HPP
#define MAIN_CLASS_HPP

template <int>
class FriendClass;

template <int dim>
class MainClass
{
public:
    MainClass(){};
private:
    
    friend FriendClass<dim>;
//  Now the global FriendClass is a friend.
    
    double private_member = 3.0;
};

#endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文