UWP C ++:只写而不是读到全局变量? [英] UWP C++: Writing only instead of reading to a global variable?

查看:111
本文介绍了UWP C ++:只写而不是读到全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单独的头文件,用于声明全局变量.我已经在每个.cpp文件中包含的pch.h中包含了该头文件. #include "variable.h"现在,我需要调用该变量并在if语句中读取它以启动一些代码.在编译之前,Visual Studio中不会显示任何错误.但是,当我编译代码时,它返回错误:

I have a separate header file for where I declare global variables. I have included that header file in pch.h which is included in every .cpp file. #include "variable.h" I now need to call the variable and read it in an if statment to start some code. Before compilation, no errors are shown in Visual Studio. However, when I compile the code, it returns error:

Error   LNK2005 "bool ahschecked" (?ahschecked@@3_NA) already defined in checkin.xaml.obj   pch.h

变量是"ahschecked",它是布尔类型,而我尝试从中读取全局变量的文件是checkin.xaml.obj,该变量的原始声明在pch.h中.

The variable is "ahschecked" which is type Boolean and the file I am trying to read the global variable from is checkin.xaml.obj, the original declaration of the variable is in pch.h

这以前在Windows窗体中有效,但是当我转移到UWP时,我无法使其正常工作.

This worked before in windows forms but when I transferred over to UWP, I was unable to get it to work.

我用一个较小的程序重新创建了这个问题.

I have recreated this issue with a smaller program.

Var.h(存储全局变量的位置)

Var.h (where the global variable is stored)

#pragma once
extern bool globalbool = false;
#pragma endregion

pch.h(自动包含在每个.cpp中)

pch.h (automatically included in every .cpp)

#pragma once
#include <collection.h>
#include <ppltasks.h>
#include "App.xaml.h"
#include "var.h"

MainPage.xaml.cpp(试图从全局变量读取)

MainPage.xaml.cpp (attempting to read from global var)

#incldue "pch.h"
#include "MainPage.xaml.h"

void testapp::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e) {
if (::globalbool == false)
{
// do something
}
}

推荐答案

我能够通过以下方法解决此问题:在头文件中声明全局变量,但不附加任何值,然后将值附加到它的外部. cpp文件.

I was able to resolve this issue by declaring the global variable in the header file without any values attached to it then attaching the values to it later outside in the .cpp file.

这篇关于UWP C ++:只写而不是读到全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆