+ -
当前位置:首页 → 问答吧 → 有关于重定义~请大家看看

有关于重定义~请大家看看

时间:2011-12-27

来源:互联网

我在global.h里面定义全局变量
下面给出接口

#ifndef __global_H
#define __global_H

//---------------------------------------------------include----------------------------------------

#include <windows.h>

//----------------------------------------------------definer------------------------------------

#define GREEN FOREGROUND_GREEN
#define RED FOREGROUND_RED
#define MAX_SIZE 100

HANDLE hOut;// get the handle of the console
WORD OriginalColor;

//----------------------------------------------------Function---------------------------------------------
void setColor(WORD Color);

void colorReset();

int mainTask();
//--------------------------------------------------------------------------------------------------------

#undef __global_H

#endif

然后在一个类里面进行了调用

***********************************************************************************************
#ifndef ...
#define....

#include "global.h"
class arithmetic
{
public:
  void show();
.....
....
private:
...
...
};
/
----------------------------
#include "global.h"
#include "arithmetic.h"
void arithmetic::show()
{
  setColor(GREEN);
  cout<<"test"<<endl;
}
#endif

***********************************************主函数********************************
#include "arithmetic.h"
#include "global.h"
#include <iostream>

using namespace std;

int main(int argc,char* argv[])
{
  arithmetic test;
  test.print();
  return 0;
}


编译错误
main.obj : error LNK2005: "unsigned short OriginalColor" (?OriginalColor@@3GA) already defined in arithmethic.obj
1>main.obj : error LNK2005: "void * hOut" (?hOut@@3PAXA) already defined in arithmethic.obj

不是调用了ifndef吗?为什么还会如此呢?

作者: judymac   发布时间: 2011-12-27

#ifndef __global_H
#define __global_H

...

#undef __global_H

#endif
最后undef干嘛???

作者: luciferisnotsatan   发布时间: 2011-12-27

#include "global.h"
这个去掉

作者: qscool1987   发布时间: 2011-12-27

引用 2 楼 qscool1987 的回复:
#include "global.h"
这个去掉




去掉还怎么调用啊~
我里面还有maxSize在class arithmetic有用到的~

作者: judymac   发布时间: 2011-12-27

引用 1 楼 luciferisnotsatan 的回复:
#ifndef __global_H
#define __global_H

...

#undef __global_H

#endif
最后undef干嘛???


undef加上也没什么关系呀~,去掉也是一样的~

作者: judymac   发布时间: 2011-12-27