简体   繁体   English

在另一个 .cpp 文件中使用命名空间定义

[英]Using a namespace define in another `.cpp` file

I have this and I can't seem to include the namespace correctly.我有这个,但我似乎无法正确包含名称空间。

main.cpp主.cpp

#include <iostream>

int main()
{
    my_space::Print(); // main.cpp:5:5: error: use of undeclared identifier 'my_space'

    return 0;
}

otherclass.cpp其他类.cpp

#include <iostream>

namespace  my_space {
    int x, y;

    void Print()
    {
        std::cout << "Hello from namespace my_space." << std::endl;
    }
}

I have tried adding a otherclass.h with namespace my_space {};我尝试添加一个otherclass.h with namespace my_space {}; in it and in main.cpp include #include "otherclass.h" but this didn't worked either.在它和main.cpp中包括#include "otherclass.h"但这也没有用。

You need to split up the declaration from the definition .您需要将声明定义分开。

Your declaration looks like this:您的声明如下所示:

namespace my_space {
  void Print();
}

Your definition looks like this:您的定义如下所示:

#include <iostream>
#include "my_space.h"

void my_space::Print() {
    std::cout << "Hello from namespace my_space." << std::endl;
}

Then you add #include "my_space.h" to your main file so it knows about the declaration.然后将#include "my_space.h"添加到主文件中,以便它知道声明。 The linker will take care of combining the final executable. linker 将负责组合最终的可执行文件。

Things like x and y need more clarification as having random global variables laying around is asking for trouble.xy这样的东西需要更多的澄清,因为随意放置全局变量是自找麻烦。

Leave your otherclass.cpp file as it is.保持 otherclass.cpp 文件不变。 Looks good.看起来不错。

Make a new otherclass.h file as you said you did, but make it look like this:按照你说的做一个新的 otherclass.h 文件,但让它看起来像这样:

#pragma once

namespace my_space {
    void Print();
}

Then build it like this (if using GCC):然后像这样构建它(如果使用 GCC):

gcc -O2 -W -Wall -std=c++17 main.cpp otherclass.cpp -o testprogram

It is important to NOT get into the habit of writing all of your function code in the header file.重要的是不要养成在 header 文件中编写所有 function 代码的习惯。 Instead learn to hide everything that you can.相反,学会隐藏你能隐藏的一切。 Notice that in my example I didn't include your x and y variables.请注意,在我的示例中,我没有包括您的 x 和 y 变量。 If those aren't needed by any other part of your program then no one else needs to know about them.如果你的程序的任何其他部分不需要这些,那么其他人就不需要知道它们了。

Code in header files adds compile time to every file that includes it. header 文件中的代码会向包含它的每个文件增加编译时间。 Worse, that code probably requires more header files to support it.更糟糕的是,该代码可能需要更多 header 个文件来支持它。 Which have to be included and compiled for every cpp file that includes the first header.必须为包含第一个 header 的每个 cpp 文件包含和编译它。

This can lead to atrocities where 500 source files each rebuild half of Boost and include Windows.h for no good reason.这可能会导致暴行,其中 500 个源文件每个都重建 Boost 的一半并无缘无故地包含 Windows.h。

In the compilation unit with main.cpp the name my_space is not declared.在带有 main.cpp 的编译单元中,未声明名称my_space So the compiler issues an error.所以编译器会报错。

You should common declarations that will be used by several compilation units to place in a header and include this header in all compilation units where a declaration from the header is used.您应该将多个编译单元使用的通用声明放在 header 中,并将此 header 包含在使用 header 声明的所有编译单元中。

As for the namespace then you could place either the declaration of the function Print in the namespace and the namespace itself place in a header. Or you could define the function inline within the namespace.至于命名空间,您可以将 function Print的声明放在命名空间中,并将命名空间本身放在 header 中。或者您可以在命名空间中内联定义 function。

As for variables then either you should declare them in the namespace with the storage specifier extern or also to declare them inline.至于变量,您应该在命名空间中使用存储说明符extern声明它们,或者也将它们声明为内联。

For example:例如:

// a header with the namespace

namespace  my_space {
    inline int x, y;

    inline void Print()
    {
        std::cout << "Hello from namespace my_space." << std::endl;
    }
}

or:要么:

// a header with the namespace

namespace  my_space {
    extern int x, y;

    void Print();
}

In the last case the corresponding definitions of the variables and of the function should be placed in some cpp file.在最后一种情况下,变量和 function 的相应定义应放在某个 cpp 文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM