(简单)头文件问题 [英] (simple) header file problem

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

问题描述

大家好,


我有以下内容:


------------文件常量。 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

推荐答案

michael写道:
michael wrote:

大家好,


我有以下内容:


------------文件constants.h ---------

#ifndef constants_

#define constants_


const int FOO = 1;

const int BAR = 2;

char * NAME =" something";

const int ARGCOUNT = 2;


# endif


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

_NAME''

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

为什么NA​​ME的问题?
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
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?



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


-

Ian Collins。

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

--
Ian Collins.


不要你有一个cpp文件作为另一个的一部分?我不是b $ b专家,但我认为_NAME的多重定义是因为

编译器获得该定义的次数更多。这可能是由于

包括头文件constants.h而导致的。来自两个(other.cpp,

crap.cpp)文件...


michael napsal:
Don''t you have one of cpp file as part as the other one? I ain''t
expert but I suppose that the multiple definition of _NAME is because
the compiler gets that definition more times. It could be due the
including the header file "constants.h" from both (other.cpp,
crap.cpp) files...

michael napsal:

大家好,


我有以下内容:


------------文件常量.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 ----------

#包括Other.h


void其他:: crap(){

//要做的东西

}

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

#includ e< iostream>

#include" constants.h"

#include" other.h"


使用namespace std;


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


MIUSS写道:
MIUSS wrote:

你不是有一个cpp文件作为另一个的一部分?我不是b $ b专家,但我认为_NAME的多重定义是因为

编译器获得该定义的次数更多。这可能是由于

包括头文件constants.h而导致的。来自两个(other.cpp,

crap.cpp)文件...
Don''t you have one of cpp file as part as the other one? I ain''t
expert but I suppose that the multiple definition of _NAME is because
the compiler gets that definition more times. It could be due the
including the header file "constants.h" from both (other.cpp,
crap.cpp) files...



请不要发帖。

Please don''t top post.


michael napsal:
michael napsal:

>>大家好,

我有以下:

------------文件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"

类其他{
public:
void crap();
};

#endif

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

void其他:: crap(){
//做什么东西
}
------ ---- file crap.cpp -----------
#include< iostream>
#include" constants.h"
#include" other.h"
使用命名空间std;

void doStuff(char * arg){
cout<< arg was << arg<< endl;
}
int main(int argc,char * argv []){
if(argc< ARGCOUNT)
doStuff(NAME);
其他
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。


--
Ian Collins.


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

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