带外部链接的命名空间 [英] Namespaces with external linkage
问题描述
我所遇到的问题基本上与'greentype'提到的
http:// www。 cplusplus.com/forum/beginner/12458/
我通过命名空间共享变量,当我尝试将我的函数定义放入考虑下面的例子,其中我想传递变量'i',在主代码中定义
到函数a():
<
* nn.h:* >
#ifndef _NN_H_
#define _NN_H_
命名空间nn {
int i;
}
#endif
* main.cpp *
#include< iostream>
#includenn.h
using namespace std;
using namespace nn;
void a();
int main()
{
i = 5;
a();
}
void a()
{
using namespace std;
using namespace nn;
i ++;
cout<< i =< i<< endl;
}
但现在,如果我把a()的定义放到一个单独的文件...
* a.cpp *
#include< iostream>
#includenn.h
void a()
{
using namespace std;
using namespace nn;
i ++;
cout<< i =< i<< endl;
}
...然后我得到'多重定义'错误时链接(g ++ main.cpp
a.cpp -o main)。如果我在头文件'extern'(如
在其他论坛中建议的)''''声明,我得到'未定义的引用'错误。我可以编译时,'i'在标题中声明为const,但这不是我想要的。
任何建议非常感谢。
任何全局对象,例如 i
使用 extern
没有初始化器使声明只是一个声明。这适合你的头文件,但你必须仍然定义 i
。除了创建头声明 extern
,还需要添加一个定义(即,声明的副本不含 extern
)到一个而且只有一个源文件。
编辑:读取您的问题,你说要传递一个变量到功能。从样式和代码结构的角度来看,这通常不是使用共享(全局)变量的好理由。在没有任何重写的原因,你通常应该定义一个函数,它接受一个参数,并通过其参数传递一个值(可能从一个局部变量)从调用站点到该函数。
The problem I have is basically the same as 'greentype' mentions at http://www.cplusplus.com/forum/beginner/12458/
I'm sharing variables through namespaces and a problem arises when I try to put my function definitions into a separate file.
Consider the following example, where I want to pass variable 'i', defined in the main code, to the function a():
* nn.h: *
#ifndef _NN_H_
#define _NN_H_
namespace nn {
int i;
}
#endif
* main.cpp *
#include <iostream>
#include "nn.h"
using namespace std;
using namespace nn;
void a();
int main()
{
i=5;
a();
}
void a()
{
using namespace std;
using namespace nn;
i++;
cout << "i = " << i << endl;
}
But now if I put the definition of a() into a separate file ...
* a.cpp *
#include <iostream>
#include "nn.h"
void a()
{
using namespace std;
using namespace nn;
i++;
cout << "i = " << i << endl;
}
... then I get 'multiple definition' error when linking (g++ main.cpp a.cpp -o main). If I make 'i' declaration in the header file 'extern' (as suggested in other forums), I get 'undefined reference' error. I can compile when 'i' is declared as const in the header, but that's not what I want.
Any suggestions greatly appreciated.
Any global object, like i
, must have exactly one definition somewhere in the program, but it can be declared multiple times.
Using extern
without an initializer makes a declaration just a declaration. This is appropriate for your header file, but you must still define i
somewhere. As well as making the header declaration extern
you also need to add a definition (i.e. a copy of the declaration without extern
) to one and only one of your source files.
Edit: Reading your question, you say that you want to pass a variable to a function. From a style and code structure point of view, this isn't usually a good reason for using a shared (global) variable. In the absence of any overriding reasons you should normally define a function which takes a parameter and pass a value (possibly from a local variable) from the calling site to that function via its parameter.
这篇关于带外部链接的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!