c++ - main() 中定义的类对象。如何在另一个类中获取此实例?
问题描述
我需要将functionA作为函数 B 中的全局函数,但只使用 main() 中定义的类 A 的一个实例。functionA将在许多类中使用,我希望尽可能简单地访问。我知道在 .h 文件中定义类是一种不好的做法,但我认为在这种情况下它并不重要。在我的情况下,我得到了错误,但我怎样才能以同样的方式做到这一点?如果我在此A a();
之前放置main()
完美,但 A 在main()
函数中被初始化。
/////A.h//////
class A
{
public:
A(){//define constructor A//};
functionA()
{
...
}
};
/////B.h//////
extern A a; //declare A//
class B
{
public:
B(){//define constructor B//};
functionB()
{
a.functionA();
}
};
////main.cpp////
#include "A.h"
#include "B.h"
int main()
{
B b(); //B b;
A a(); //A a; of course. Thanks for remark guys.
}
这是我得到的错误:
Error LNK2001: unresolved external symbol "class A a"
解决方案
推荐阅读
- java - 有什么办法可以在android studio中为android时钟设置多个闹钟?
- reactjs - 使用 React 在 Sendbird 中创建默认聊天组
- vba - 将电源查询插入宏时出现多个错误,“名称查询已存在”,“范围类的选择方法失败”
- ios - iPadOS 15 的收盘率和评论冻结界面
- javascript - 一个使用 javascript 和 ajax 的简单 POST 示例
- php - 原因:“未捕获的错误:在 null 上调用成员函数 is_empty()”
- java-8 - 不存在变量 R 的实例,因此 void 符合 R
- google-cloud-platform - GCP Compute Engine 免费试用:e2-medium
- sql - 我需要帮助重写 2 个数据库查询,这些查询最初是用名为 squeel 的 gem 编写的
- java - Spring Boot - 如何在拦截器中清理路径变量