类在编译时相互干扰
我正在开发一个 C++ 项目。
我有一个带有其函数的类,然后我意识到其中一些函数与该类无关,而只是数学函数,因此我决定将它们移至命名空间。
我的第一个问题是,C++ 命名空间的适当文件扩展名是什么?
我有一个 Constants.h 文件,我计划在其中保存全局常量,例如 PI。
现在:
#include <math.h>
const double PI = M_PI;
我有我之前讨论过的命名空间,现在称为:specificMath.h
#include <stdlib.h>
#include "constants.h"
... more code
我有一个 gaussian.cpp:
#include "gaussian.h"
#include "specificMath.h"
#include <iostream>
... more code
该文件包含一个现在不执行任何操作的主函数,我只是无法编译整个项目没有主...
我有一个 gaussian.h ,其中我没有包含任何内容,这是错误的吗?
第三个类,没有属性,只有方法(同样,这是错误的吗?或者不漂亮?)。 truncatedFunctions.cpp
#include "specificMath.h"
#include <stdlib.h>
#include "truncatedFunctions.h"
#include "gaussian.h"
using namespace specificMath;
及其 truncatedFunctions.h 中,我再次没有包含任何内容。
还有第四个类,
#include "margin.h" //Its header, where I'm not including anything
#include "gaussian.h"
#include "specificMath.h"
using namespace specificMath;
当我“制作”它时,它似乎编译得很好,但是当它到达链接部分时,我收到很多错误,说我的 margin.cpp 类上的内容首先在 truncatedFunctions.cpp 中定义
我快要疯了。我不知道为什么会发生这种情况,也不知道如何解决。如果有人可以帮助我,我将非常感激,并且任何额外的建议都会很棒,因为我真的想通过这个项目尽可能多地学习。谢谢!
I'm working on a C++ project.
I had a class with its function, then I realized some of those functions weren't related to that class but were just math functions so I decided to move them on to a namespace.
My first question is, what is the appropriate file extension for a c++ namespace?
I have a constants.h file where I plan on saving global constants, for example PI.
Right now:
#include <math.h>
const double PI = M_PI;
I have the namespace I talked about before, right now is called: specificMath.h
#include <stdlib.h>
#include "constants.h"
... more code
I have a gaussian.cpp:
#include "gaussian.h"
#include "specificMath.h"
#include <iostream>
... more code
This file includes a main function that right now does nothing, I just can't get the whole project to compile without a main...
I have a gaussian.h where I'm not including anything, is that wrong?
A third class, which has no attributes, just methods (again, is this wrong? or not pretty?). truncatedFunctions.cpp
#include "specificMath.h"
#include <stdlib.h>
#include "truncatedFunctions.h"
#include "gaussian.h"
using namespace specificMath;
And its truncatedFunctions.h where, again, I'm not including anything.
And a fourth class where I include
#include "margin.h" //Its header, where I'm not including anything
#include "gaussian.h"
#include "specificMath.h"
using namespace specificMath;
When I "make" it, it seems to compile fine, but when it gets to the linking part I get A LOT of errors saying that things on my margin.cpp class were first defined in truncatedFunctions.cpp
I am going crazy. I have no idea why this is happening, or how to solve it. I would really appreciate if somebody could help me out, and please, any extra piece of advice would be great since I am really trying to learn as much as I can with this project. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你定义了吗 /em> 你的特定Math.h 中的函数?您应该只声明这些函数。
例如,如果您的 SpecificMath.h 包含类似的函数定义
,并且您正在使用将此文件包含在其他几个文件中,则链接器会变得疯狂。 包括意味着复制。因此,您已经多次定义了
coolstuff::print
。更好的方法(也是在许多文件中使用自编写函数时唯一可能的方法)是将代码拆分为标头和实现,就像在 gaussian 中所做的那样。当您包含 Coolstuff.namespace.h 时,它只会声明函数。并且您可以多次声明同一个函数。
.cpp 文件包含函数的实现。现在您的链接器不会感到恼火,因为
coolstuff::print
只有一种实现,而不是 n (其中 n 是#include "cs.namespace.h"
的数量代码>你使用的)。没有标准的命名空间扩展。使用 .h/.cpp 作为标头/实现和自定义前缀,例如“nmspc”或“nsc”。由你决定。
Did you define your functions in your specificMath.h? You should only declare those functions.
For example, if your specificMath.h contains function definitions like
and you are using including this file in several others, the linker is going crazy. Including means copying. And so you've got yourself
coolstuff::print
defined several times. The better way (and the only possible way when using self-written functions in many files) is splitting your code into a header and implementation as you did in gaussian.When you include coolstuff.namespace.h it will only declare functions. And you can declare the same function several times.
The .cpp file contains the implementation of your functions. Now your linker won't get irritated because there is only one implementation of
coolstuff::print
and not n (where n is the number of#include "cs.namespace.h"
you used) ones.There is no standard namespace extension. Use .h/.cpp for header/implementation and a self-defined prefix, something like 'nmspc' or 'nsc'. It's up to you.
很难判断您的代码中是否做错了什么(因为您没有显示任何错误),但首先要尝试的是“清理”您的构建并重建您的所有代码。如果编译器(不知道您正在使用什么)由于某种原因没有编译所有修改的模块,那么链接器遇到问题也就不足为奇了。
在 C++ 中,头文件通常是
.h
或.hpp
。这对编译器来说并不重要。一般来说,最好先
#include
标准库中的内容,然后再添加您自己的内容。不。如果您不需要包含任何内容,就不要包含任何内容。
It's hard to tell whether you've done anything wrong in your code (because you didn't show any of it), but the first thing to try is to "clean" your build and rebuild all your code. If the compiler (don't know what you're using) for some reason didn't compile all your modified modules, then it's not surprising that the linker is having trouble.
In C++, header files are usually
.h
or.hpp
. It doesn't matter to the compiler.In general, it's a good idea to
#include
stuff from the standard library first, followed by your own things.No. If you don't need to include anything, don't include anything.
首先,对标头使用包含防护。
这将防止相同的标头在同一翻译单元中包含两次。
其次,不要在标头中定义不合理的内容:
这将导致所有翻译单元导出该符号。
在标头中声明变量
extern
并在实现文件中为其提供定义。First, use include guards for the headers.
This will prevent the same header from being included twice in the same translation unit.
Second, don't define uncosnt stuff in the headers:
this will cause all translation units to export that symbol.
Declare the variable
extern
in your header and provide a definition for it in an implementation file.