来自C ++中不同类的成员函数的数据访问

岩石

我很困惑如何从另一个类实现数据成员访问。我在三个不同的头文件中有三个类。

A.h
#include "B.h"
#include "C.h"
class A{

    B *b;
    friend void dataaccess_b_from_class_A(int a);
}

B.h
class B{

}

C.h
class C{

   void dataaccess_b_from_class_A(int a);
}
void C::dataaccess_b_from_class_A(int a)
{
   b = new B(); //I got error as symbol b could not be resolved.
}

我喜欢dataaccess_b_from_class_A()从方法class C访问数据B *bClass A我把朋友功能放进去了class A,但是我知道了。error as symbol b could not be resolved我该怎么实现呢?

编辑1:根据讨论“ gmas80”,我所做的是

class B; // forward declaration

class A{
public:
    A(){};
private:
    static B* b; // since in the dataaccess_to_class_A you are using new
    friend class C; // this make b and dataaccess_from_class_C accessible
};

class B{
public:
    B(){ cout << "B" << endl; };
   // add content
};

class C{
public: // you need this keyword if you want to call this function from outside
   void dataaccess_to_class_A(A a);
};

void C::dataaccess_to_class_A(A a)
{
   A::b = new B(); //Error as undefined reference to class A::b

   cout << "C" << endl; // test if called
}

如果不包括static,则得到b could not be resolved如果包含static,则得到undefined reference谢谢

gmas80

您在这么小的代码中包含了很多错误!为什么不从简单开始?这是编译后的代码的修改后的单文件版本:

#include <iostream>
using namespace std;

class B; // forward declaration

class A{
public:
    A(){};
private:
    B* b; // since in the dataaccess_to_class_A you are using new
    void dataaccess_from_class_C(){ cout << "A" << endl; }; // test if called
    friend class C; // this make b and dataaccess_from_class_C accessible
};

class B{
public:
    B(){ cout << "B" << endl; };
   // add content
};

class C{
public: // you need this keyword if you want to call this function from outside
   void dataaccess_to_class_A(A a);
};

void C::dataaccess_to_class_A(A a)
{
   a.b = new B(); // this is a potentially memory leak if you will not delete in somehow
   a.dataaccess_from_class_C();
   cout << "C" << endl; // test if called
}

// it is better if you post runnable code
int main() {
    C c;
    A a;
    c.dataaccess_to_class_A(a);
}

编辑后1

现在,您可以开始将类移动到头文件中,但是您需要添加监护人来避免多个定义。

#ifndef H_GUARDIAN_A
#define H_GUARDIAN_A

#include <iostream>
using namespace std;

class B; // forward declaration

class A
{
public:
    A()
    {};
private:
    B* b; // since in the dataaccess_to_class_A you are using new
    void dataaccess_from_class_C()
    {
        cout << "A" << endl;
    }; // test if called
    friend class C; // this make b and dataaccess_from_class_C accessible
};

class B
{
public:
    B()
    {
        cout << "B" << endl;
    };
    // add content
};

class C
{
public: // you need this keyword if you want to call this function from outside
    void dataaccess_to_class_A( A a );
};

void C::dataaccess_to_class_A( A a )
{
    a.b = new B(); // this is a potentially memory leak if you will not delete in somehow
    a.dataaccess_from_class_C();
    cout << "C" << endl; // test if called
}

#endif

main.cpp

#include "a.h"

// it is better if you post runnable code
int main()
{
    C c;
    A a;
    c.dataaccess_to_class_A( a );
}

这对您有意义吗?我简单地将类声明移到了包含的另一个文件中。

编辑2

现在,我们将类定义分为三个不同的标题。

#ifndef H_GUARDIAN_A
#define H_GUARDIAN_A

#include <iostream>
using namespace std;

class B;

class A
{
public:
    A(): b(NULL){}; // initialize to NULL the b pointer
    ~A(); // new entry: destructor to eventually delete b (only member function declaration)
private:
    B* b; // since in the dataaccess_to_class_A you are using new
    void dataaccess_from_class_C()
    {
        cout << "A" << endl; // test if called
    }; 
    friend class C; // this make b and dataaccess_from_class_C accessible
};

#endif

a.cpp //新条目!它避免了循环依赖..析构函数在这里定义

#include "a.h"
#include "b.h"

A::~A() // destructor that eventually clean memory for b
{
    if( b ) delete b;
}

h

#ifndef H_GUARDIAN_B
#define H_GUARDIAN_B

#include "a.h"

class B
{
public:
    B()
    {
        cout << "B" << endl;
    };
    // add content
};

#endif

ch

#ifndef H_GUARDIAN_C
#define H_GUARDIAN_C

#include "b.h"

class C
{
public: // you need this keyword if you want to call this function from outside

    void dataaccess_to_class_A( A a );
};

void C::dataaccess_to_class_A( A a )
{
    a.b = new B(); // this is a potentially memory leak if you will not delete in somehow
    a.dataaccess_from_class_C();
    cout << "C" << endl; // test if called
}

#endif

main.cpp

#include "c.h"

// it is better if you post runnable code
int main()
{
    C c;
    A a;
    c.dataaccess_to_class_A( a );
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在C ++中访问不同类中的类成员

来自分类Dev

不同类的成员函数指针的C ++映射

来自分类Dev

C ++来自不同类的多个回调成员函数,没有std和boost

来自分类Dev

如何在C ++中处理具有多个不同类型的数据成员的类?

来自分类Dev

来自不同类的 C# 单个列表

来自分类Dev

如何在字典中存储来自 C# 中不同类的不同类型的方法

来自分类Dev

来自静态成员函数的sizeof C ++类

来自分类Dev

爪哇。运行来自不同类的函数

来自分类Dev

如何在C ++中跨成员函数访问成员变量

来自分类Dev

C++:如何存储不同类的各种成员函数以备后用

来自分类Dev

在python中传递来自不同类的值

来自分类Dev

汇编中来自C的数组

来自分类Dev

来自C的Lua中的OOP

来自分类Dev

在C中来自.txt的Fscanf

来自分类Dev

来自C的Lua中的OOP

来自分类Dev

在 C# 中解析来自 json 的数据

来自分类Dev

访问和汇总来自不同类django的选择

来自分类Dev

无法访问来自不同类别的变量

来自分类Dev

使用来自函数(MPI_comm_size / rank)的数据初始化C ++ const成员

来自分类Dev

来自其他类的C ++成员函数指针

来自分类Dev

来自不同类的IOS Swift调用函数变为nil

来自分类Dev

从私有结构数据成员访问C ++类的公共成员函数

来自分类Dev

来自json数据的AngularJS动态表单(不同类型)

来自分类Dev

如何在C#中的单个对象数组中访问不同类型的类

来自分类Dev

如果格式不同,C如何解释来自联合的数据?

来自分类Dev

如果格式不同,C如何解释来自联合的数据?

来自分类Dev

C#-来自Woocommerce rest API的数据

来自分类Dev

C#类声明“来自元数据”

来自分类Dev

C#类声明“来自元数据”

Related 相关文章

热门标签

归档