本文介绍了(简单)头文件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有以下内容:


------------文件常量。 h ---------

#ifndef constants_

#define constants_


const int FOO = 1;

const int BAR = 2;

char * NAME =" something";

const int ARGCOUNT = 2;


#endif


----------- file other.h -------------

#ifndef other_

#define other_


#include" Constants.h"


class其他{

public:

void crap();

};


#endif


----------- file other.cpp ----------

#include " Other.h"


void其他:: crap(){

//做东西的事情

}

----------文件crap.cpp -----------

#include< iostream>

#include" constants.h"

#include" other.h"


using namespace st d;


void doStuff(char * arg){

cout<< arg was << arg<<结束;

}


int main(int argc,char * argv []){

if(argc< ARGCOUNT )

doStuff(NAME);

else

doStuff(argv [1]);

}

------------------------------------


当我编译时,编译器抱怨我有多个定义

_NAME''

它对我定义的其他变量没有问题Constants.h,所以

为什么NA​​ME出现问题?


这似乎与char *有关,但我无法想象出什么

或为什么.....


感谢您的帮助


Michael

Hi All,

I have the following:

------------ file constants.h---------
#ifndef constants_
#define constants_

const int FOO = 1;
const int BAR = 2;
char* NAME = "something";
const int ARGCOUNT = 2;

#endif

----------- file other.h -------------
#ifndef other_
#define other_

#include "Constants.h"

class Other {
public:
void crap();
};

#endif

----------- file other.cpp ----------
#include "Other.h"

void Other::crap(){
//stuff to do stuff
}
---------- file crap.cpp -----------
#include <iostream>
#include "constants.h"
#include "other.h"

using namespace std;

void doStuff(char* arg){
cout << "arg was " << arg << endl;
}

int main(int argc, char* argv[]){
if(argc < ARGCOUNT)
doStuff(NAME);
else
doStuff(argv[1]);
}
------------------------------------

When I compile, the compiler complains that I have ''multiple definition of
_NAME''
It has no problem with the other variables I have defined in Constants.h, so
why the problem with NAME?

It seems to be something to do with the char*, but I can''t figure out what
or why.....

Thanks for your help

Michael

推荐答案



其他变量是常数,为什么不是吗?


-

Ian Collins。

The others are constants, why isn''t NAME?

--
Ian Collins.





请不要发帖。

Please don''t top post.




-

Ian Collins。


--
Ian Collins.


这篇关于(简单)头文件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 20:58