在头文件中声明全局变量时出现未解决的外部符号错误 [英] Unresolved external symbol error when declaring global variables in header file

查看:170
本文介绍了在头文件中声明全局变量时出现未解决的外部符号错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我的主代码使用了多个头文件和源文件.我在头文件中用关键字extern声明了全局变量,然后在我的一个源文件中定义了这些变量.一些我的源文件正在使用这些全局变量.我已通过以下方式将标头保护添加到我的所有标头文件中.

我在头文件basic.h的顶部添加了这些命令
#ifndef BASIC_H
#define BASIC_H

最后我添加了.
#endif

我正在使用Visual c ++ 2008.当我编译程序时,我遇到了6个不同的无法解析的外部符号错误

错误4错误LNK2001:未解析的外部符号&"int oilvariables&" (?oilvariables @@ 3HA)basic.obj

我还通过以下链接在mediafire上传了我的项目.
http://www.mediafire.com/?whzjagw3waio9qi [

Hello,
I have mutiple header files and source files that my main code is using.I have declared my global variables with keyword extern in my header file,and then I have define these variables in my one of source file.Some of my source files are using these global variables.I have added header guard to all of my header file in the following way.

I have added at top of header file basic.h these commands
#ifndef BASIC_H
#define BASIC_H

and at end I have added.
#endif

I am using Visual c++ 2008.When I compile my program I get 6 different errors of unresolved external symbol

Error 4 error LNK2001: unresolved external symbol "int oilvariables" (?oilvariables@@3HA) basic.obj

I have also uploaded my project at mediafire at the following link.
http://www.mediafire.com/?whzjagw3waio9qi[^]
If anyone knows the answer please help me..........
Bundle of thanks in advance

I have also added my header and source files here.
UOV.cpp

#include "basic.h"
#include "matrixb.h"
using namespace std;
int main() {
	cout<<"\nOil Variables\n";
	cin>>oilvariables;
	cout<<"\nVineagar Variables\n";
	cin>>vinegarvariables;
return 0;
}



basic.h



basic.h

#ifndef BASIC_H
#define BASIC_H

#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <ctime>
#include<fstream>
#include <direct.h>

extern int oilvariables;
extern int vinegarvariables;
extern int n;//Oilvariables+VinegarVariables
extern int D;  //number of quadratic terms in a public polynomial
#endif



basic.cpp



basic.cpp

#include "basic.h"

int n=oilvariables+vinegarvariables;
int D=(n*(n+1))/2;



matrixb.h



matrixb.h

#ifndef MATRIXB_H
#define MATRIXB_H

#include "basic.h"
extern int** B_LRS;//Matrix B generated by LRS

#endif



matrixb.cpp



matrixb.cpp

#include "matrixB.h"



using namespace std;
int** B_LRS=0;

推荐答案

首先:请勿使用全局变量...它们的易于使用"只会导致大量问题.相信我,我维护了公司中以前一个人制作的软件.他喜欢全局变量,因此我花了几个月的时间来理解一些奇怪的错误.

即使您仍想使用它们:

-确保在.h
中使用extern关键字 -确保本地变量(在文件或函数中)的名称不相同.
-确保.cpp中的变量类型与其外部声明的类型匹配.
-确保包含变量的.cpp文件是项目的一部分.

如果这样不能解决问题.给出一些代码.

------

好的.您在 basic.cpp 中定义了nD,但是您仍然需要定义oilvariablesvinegarvariables:我在您的代码中看不到它们.

------

您正在从用户那里获取值,但是这些值将保留在哪里?您需要像其他变量一样定义变量(例如,在basic.cpp中):
First of all: DO NOT USE GLOBAL VARIABLES... Their "ease of use" just lead to tons of problems. Believe me, I maintained a sofware made by a previous guy in my company. He liked global variables and because of that it took me months to understand some strange bugs.

Even though you still want to use them:

- Make sure the extern keyword is used in the .h
- Make sure a local variable (in your file or function) has not the same name.
- Make sure the type of the variable in the .cpp matches the type of its extern declaration.
- Make sure the .cpp file that contains the variable is part of your project.

If this doesn''t solve the problem. Give some code.

------

OK. You defined n and D in basic.cpp but you still need to define oilvariables and vinegarvariables: I don''t see them in your code.

------

Your are taking the values from the user, but where the values will be kept? You need to define the variables like the other ones (in basic.cpp for example):
int oilvariables;
int vinegarvariables;
int n = ...;
int D = ...;




我将建议再有一段时间不使用全局变量. 您的代码中不需要它们.改用函数,参数和函数中的返回值.




I will suggest one more time to not use global variables. You don''t need them in your code. Use functions, parameters, and return values from your functions instead.


未解析的外部符号"错误是链接器错误,表示您已引用与链接器一起使用的目标文件中未定义的变量或函数.出现此错误的原因有很多,但最常见的原因是您忘记定义变量或函数了.

在您的情况下,您需要定义您的变量,例如:
The "unresolved external symbol" error is a linker error saying that you have referenced a variable or function that is not defined within the object files the linker is fed with. This error can pop up for a number of reasons, but most often it''s because you have forgotten to define the variable or function.

In your case you need to define you variables like:
int oilvariables;
int vinegarvariables;
int n;
int D;


您的cpp文件之一中的某处.

如果您需要访问全局属性,建议您使用
Singleton模式 [


somewhere in one of your cpp files.

If you need to have access to global properties, I suggest you use a Singleton Pattern[^] to implement the functionality needed. A singleton class allows you to access one, and only one, instance of a class where you can put your properties, thus simulating ''global'' behavior without the drawbacks of true global variables.


请勿使用全局变量.做类似的事情:

Do not use global variables. Do something like:

using namespace std;
int main()
{
    int oilvariables = 0, vinegarvariables = 0;
    cout << "\nOil Variables\n";
    cin >> oilvariables;
    cout << "\nVineagar Variables\n";
    cin >> vinegarvariables;

    int n = oilvariables + vinegarvariables;
    int D = (n * (n + 1)) / 2;

    //now you can do whatever you want with your 4 LOCAL variables.
    ...

    return 0;
}


这篇关于在头文件中声明全局变量时出现未解决的外部符号错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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