gpt4 book ai didi

c++ - 从 C++ 中不同类的成员函数访问数据

转载 作者:行者123 更新时间:2023-11-28 06:37:07 25 4
gpt4 key购买 nike

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

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.
}

我喜欢 class C 中的 dataaccess_b_from_class_A() 方法访问 Class A 中的数据 B *b。我将友元函数放在 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
}

如果我不包括静态的,我会得到 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 之后

现在你可以开始在头文件中移动类了,但是你需要添加监护人以避免多次定义..

a.h

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

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

EDIT2

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

a.h

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

b.h

#ifndef H_GUARDIAN_B
#define H_GUARDIAN_B

#include "a.h"

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

#endif

c.h

#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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com