为队列创建C 全局变量
Creating c++ global variable for a queue
注意:此问题在另一个帖子中解决。该职位的链接包括在底部。
我正在使用Visual Studio 2017 Community Edition。我有一个队列,我想成为一个全局变量。
我的主要例程看起来像:
#include "TestClass.h"
using namespace std;
extern queue<Message*> g_incoming;
int main()
{
PFRHelloMessage* msg1 = new PFRHelloMessage( 0, 1, 2, 30, 40, 1500, 45, 17);
g_incoming.push(msg1);
DropLinkMessage* msg2 = new DropLinkMessage( 1, 3, 4);
g_incoming.push(msg2);
AddLinkMessage* msg3 = new AddLinkMessage( 2, 5, 6);
g_incoming.push(msg3);
//while (!g_incoming.empty())
//{
// Message* m = g_incoming.front();
// g_incoming.pop();
// cout << m->getType() << endl;
//}
TestClass* tc = new TestClass();
tc->dump();
return 0;
TestClass看起来像这样:
#include "TestClass.h"
#include <queue>
#include "Message.h"
extern queue<Message*> g_incoming;
void dump()
{
cout << "start: externally accessed" << endl;
//while (!g_incoming.empty())
//{
// Message* m = g_incoming.front();
// g_incoming.pop();
// cout << m->getType() << endl;
//}
cout << "end: externally accessed" << endl;
}
我会告诉我的错误,我有两个未解决的外部变量。(当然,这不是真正的程序。我显然在这里不需要外部变量。我需要弄清楚它如何适用于真实程序。(
解决方案:这个问题的每个评论都提供了一些有用的信息;然而,我发现这是一个重复的问题,并且在此处是完整示例答案的链接:何时在C 中使用外部我已经使用自己的课程实现了这一点,并且可以正常工作。我不确定为什么我昨天没有看到(或也许没有注意到(。
我想这是一个链接器问题。只需将定义放入.cpp文件(官方外部(中:
queue<Message*> g_incoming;
相关文章:
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 当vector是tje全局变量时,c++中vector的内存管理
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 内联函数中具有内部链接的全局变量
- 如何在信号处理程序和普通函数中对全局变量进行互斥读写操作
- 全局变量 多读取器 一个写入器多线程安全?
- 如果全局变量默认是外部变量,为什么要添加"extern"关键字?
- std::线程不是全局变量,但在到达创建它的函数的末尾时不会超出范围?
- Qt C++创建一个所有类都可以访问的全局变量
- 我真的可以使用 Type 的每个声明创建一个全局变量吗?
- 为队列创建C 全局变量
- 如何在任何其他全局变量或静态变量之前创建/构造类实例
- 如何创建用于多个源文件的全局变量/包含/函数
- 在 llvm 中创建全局变量时出错
- 要创建持久变量,最好是使用局部静态变量还是全局变量
- 在函数C++中创建全局变量
- 从C++源文件创建全局变量列表
- 如何创建一个具有全局变量的 lib 文件,这些全局变量应该将其信息共享给另一个项目
- 创建构造函数引用全局变量的类的全局实例时,C++ 程序崩溃
- 在C++函数中创建数组,而不使用全局变量