gpt4 book ai didi

c++ - C++ 中的外部:对于外部 VarX 变量::错误 LNK2001:未解析的外部符号 "unsigned int VarX"(?VarX@@3IA)

转载 作者:行者123 更新时间:2023-11-30 02:04:43 25 4
gpt4 key购买 nike

作为我之前在 stackoverflow 中的问题的延续: Getting LINK error : Extern in C++. How to access the value of a variable which is modified in File A.CPP in another file File B.CPP在我的 C++ 代码中,我想在文件“B”中使用变量“VarX”,该文件实际上在另一个文件“A”中被修改。所以我看了一下@以下链接并使用了外部概念。

How do I use extern to share variables between source files?

error LNK2001: unresolved external symbol "unsigned int VarX" (?VarX@@3IA)

我的场景如下:

File1.h
extern unsigned int VarX;

File2.cpp
#include File1.h
VarX = 101;

File3.cpp
#include File1.h
unsigned int temp = VarX;

IMP 注意:在头文件 File1.h 中,除了 Extern 定义之外,还有许多其他结构定义和许多其他定义。

有人可以帮助我吗?如何在另一个文件 File3.cpp 中读取在 File2.cpp 中修改的 VarX 的值。

最佳答案

您必须在全局范围内定义 VarX,我假设您现在没有这样做,否则它甚至无法编译:

//File2.cpp
#include "File1.h"
unsigned int VarX = 101; //this has to be outside any code block or namespace
//or class...

关于c++ - C++ 中的外部:对于外部 VarX 变量::错误 LNK2001:未解析的外部符号 "unsigned int VarX"(?VarX@@3IA),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10299296/

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