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

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 *b中的数据Class 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
谢谢

最佳答案

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

#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;
}


身高

#ifndef H_GUARDIAN_B
#define H_GUARDIAN_B

#include "a.h"

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

#endif




#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 );
}

关于c++ - 来自C++中不同类的成员函数的数据访问,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26600340/

10-12 00:37
查看更多